Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- Clint Pachl
September 28, 2004, 3:20 pm
rate this thread
I am trying to remove a name=value pair from a url's query string
using php's preg_replace.
Here's what I am trying to accomplish:
// query string tests
$qs = 'var2=two&var1=one&var3=three'; // start
$qs = 'var1=one&var2=two&var3=three'; // middle
$qs = 'var1=one&var3=three&var2=two'; // end
// re - here's my best guess
preg_replace( '/&(?=(?:$))(var2=[^&]*)&?/', '', $qs );
// the output I'd like from preg_replace inputting any of the three $qs
Any help appreciated.
Re: Regular Expression: Backreference Inside Look-ahead
This, I believe, always fails. When the back reference is
reached, its subpattern hasn't yet captured anything. So
you're in effect looking ahead for an empty string that is
at the end  of the subject string; but after that you're
You don't need lookaheads:
First match and capture an optional ampersand, followed by
'var2=' and zero or more characters excluding ampersands.
Then, if the optional ampersand matched, don't match
anything else; if it didn't, match an ampersand.
 'End' meaning, by default, either the very end or right
before a newline that is the last character.