Search code examples
phpregexpreg-replaceentities

Replace all & with & only if not followed by amp;


Ok, i'm not that smart in regexp, here's what i'm simply trying to do

basically, i have a string with some already encoded entities, but i need to also encode ALL entities again EXCEPT the & entity, so if we have a string

The Sun & Mars are planets

It will stay the same, because we don't need to encode & in & again

But if we'll have

The Sun — big hot planet

This should become

The Sun — big hot planet

I know it's silly, but that's what one parser wants..


Solution

  • Just do two substitutions:

    1. Replace & with &
    2. Replace & with &