Search code examples
phpregexpreg-replacespecial-characters

Preg_replace special characters &


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!


Solution

  • You do not need to escape & only the ?

    $reg = preg_replace("#&&#", "&", $reg);
    
    $reg = preg_replace("#\?&#", "?", $reg);