RE: Negative Look Ahead Regex

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi

I do not know if this is possible using negative assertions, but you can do
it with a search for any links, and then applying a callback which will
conditionally add your session id.

This seems to do the trick:

function addid($href){
$id = "?PHPSESSID=t9gksvpdcuobsnqt98qloe6lg4;";
$href = $href[1];
if(!strpos($href, "PHPSESSID")) $href = preg_replace("/^([^#?]*)\??/",
"$1$id", $href);
return $href."\"";
}
$buffer = preg_replace_callback("/(href=\".*?)\"/", "addid", $buffer);


Simcha Younger

-----Original Message-----
From: Cameron B. Prince [mailto:cplists@xxxxxxxxxxxxxxxxxx] 
Sent: Monday, August 18, 2008 4:41 PM
To: Simcha Younger; php-general@xxxxxxxxxxxxx
Subject: Re:  Negative Look Ahead Regex - Code Mistake or Bug?

Hi Simcha,

I tried your suggestion and it does prevent the SID from being inserted
inside the extension, but causes double ?'s and SID's in some cases.

Thanks,
Cameron


On 8/18/08 2:25 AM, "Simcha Younger" <simcha.younger@xxxxxxxxx> wrote:

> 
> Hi
> 
> 
> You did not put a question mark in your character class  ([\/\w\.-] and
> instead you put it together with the session id.
> The expression looks for a sequence without which is not followed by the
> next expression --- ?PHPSESSID\=2u0cca.....
> If it would count the last `p` of `.php` then the next expression would
> violate the negative lookahead.
> Instead it stops just before it, after the `.ph` which is the longest
> expression it could find that fulfills your negative lookahead, and then
it
> inserts the new sessionid over there.
> 
> Try instead 
> $buffer =
>
preg_replace('/"http\:\/\/www\.domain\.com([\/\w\.\-]*\??)(?!PHPSESSID\=2u0c
> caffoh6jaeapkke35qpp87;?)/',
> '"http://www.domain.com$1?PHPSESSID=t9gksvpdcuobsnqt98qloe6lg4;',
$buffer);
> 
> (not tested)
> 
> 
> 
> Simcha Younger
> 
> -----Original Message-----
> From: Cameron B. Prince [mailto:cplists@xxxxxxxxxxxxxxxxxx]
> Sent: Monday, August 18, 2008 12:56 AM
> To: php-general@xxxxxxxxxxxxx
> Subject:  Negative Look Ahead Regex - Code Mistake or Bug?
> 
> Hello,
> 
> I¹ve run into a problem with a regex and need help determining if this is
my
> mistake or a bug. The regex is for inserting a SID into every link in the
> buffer before it¹s flushed, but only when each link doesn¹t already have a
> SID.
> 
> An example of the code is here:
> 
> $buffer = 
>
preg_replace('/"http\:\/\/www\.domain\.com([\/\w\.-]*)(?!\?PHPSESSID\=2u0cca
> ffoh6jaeapkke35qpp87;?)/',
> '"http://www.domain.com$1?PHPSESSID=t9gksvpdcuobsnqt98qloe6lg4;',
$buffer);
> 
> The code works correctly in most cases except when a SID already exists
and
> there is a path after the .com such as:
> 
>
http://www.domain.com/path1/path2.php?PHPSESSID=2u0ccaffoh6jaeapkke35qpp87;
> 
> When this URL is processed by the regex, it becomes:
> 
>
http://www.domain.com/path1/path2.ph?PHPSESSID=t9gksvpdcuobsnqt98qloe6lg4;p?
> PHPSESSID=2u0ccaffoh6jaeapkke35qpp87;
> 
> 
> So the problem is the regex not only falsely matches, it's inserting the
SID
> in between 2nd and 3rd letters of the extension.
> 
> You can see the code in action here:
> 
> http://kottmann.com/test.php
> 
> The full source for the test code is also available when viewing the
source
> of this page.
> 
> I've tested this on PHP v5.1.6 and PHP v5.2.6.
> 
> TIA,
> Cameron
> 
> 
> 
> 
> 
> 
> 



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

No virus found in this incoming message.
Checked by AVG - http://www.avg.com 
Version: 8.0.138 / Virus Database: 270.6.4/1617 - Release Date: 17/08/2008
12:58


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux