Good afternoon,
I have an error in my preg_replace. I would like to replace &&
with only one &
, and ?&
with ?
.
My code looks like this:
$reg = preg_replace("#\&\&#is", "&
", $reg);
$reg = preg_replace("#\?\&#is", "?
", $reg);
Could you please help me fix this? I am sure, it some basic error, so sorry for that...
Thanks!
You do not need to escape &
only the ?
$reg = preg_replace("#&&#", "&", $reg);
$reg = preg_replace("#\?&#", "?", $reg);
You can simplify the two regexs into one.
echo preg_replace("#([?&])\s*&#", "$1", ' ? &lang=en');
Output:
?lang=en
Your modifiers didn't make sense since you aren't using alpha characters or the .
.
Also &
isn't a special regex character, just ?
. If in a character class ([]
) neither will need to be replaced.
Regex101 Demo: https://regex101.com/r/iS4mQ0/1