I have a form input reading the current URL using REQUEST_URI. How would I remove the empty variables?
Removing name http://www.phpbuilder.com?id=2&name=&f=10
$newString = preg_replace('#(?<=&|\?)[^=&]+=(&|$)#', '', $oldString);
PS: When you figure out how/why that works, you've passed your regular expression quiz for the day. 🙂
The regex is definitely way too advanced for me but I'll try and take a blind crack at it.
This is going to take awhile for me to learn. Thanks NogDog
Not bad . . .
Probably the trickiest part is using the (?<= look-behind assertion so that the leading "?" or "&" is not part of the string that gets replaced. Then the final parenthesized sub-pattern is looking for either a "&" character or an end-of-string assertion ("$") to terminate the string to be replace.
I'm going to have to read this over a couple of times. Thanks NogDog
A good book on regular expressions is Mastering Regular Expressions By Jeffrey Friedl.
His book explains all of what you see above.
But your brain will still hurt.
Jeff