tags:

views:

66

answers:

1

I attempted to answer this question (answer deleted, so here is my code).

<?php
function remove_get_param($uri, $name) {  
        return preg_replace('/(?<=\?|&|;)' . preg_quote($name, '/') . '=[^&;]*/', '', $uri);     
}

My initial code was consuming the first param such as ? when it shouldn't. I tried to do a lookbehind assertion but PHP said...

Warning: preg_replace(): Compilation failed: nothing to repeat at offset 11 on line 4

I'm relatively new with regex assertions, but I assumed that a lookbehind assertion means make sure this pattern precedes, but don't consume it as part of the match.

I looked up the syntax by googling regex cheetsheat and the resulting PNG I downloaded said that the syntax was ?<=.

What am I doing wrong?

Thanks

Update

Hello again. Here is some example usage that caused the warning above twice...

echo remove_get_param('http://mysite.com?param1=1&amp;param2=2', 'param2') . "\n";
echo remove_get_param('http://mysite.com?param1=1&amp;param2=2', 'param1'); 

I should also mention I got these errors on codepad.org. Sorry for failing to mention that, I know codepad.org runs in a funky environment.

+2  A: 

The code which you were testing on Codepad has an additional asterisk after the lookbehind:

return preg_replace('/(?!=\?|&|;)*' . preg_quote($name) . '=[^&;]/', '', $uri);
                              // ^-- problem character

Since there is nothing for the 0 or more repetition to be applied to, the regular expression compilation fails with the mentioned error. It looks like that asterisk was the one you meant to be present at the end of the expression, as seen in the example in your question.

Tim Stone
I win today's stupid Stack Overflow question! :)
alex
@alex: Heh, it happens to all of us. This is actually the third typo-related answer I've given this week. :)
Tim Stone