Alex K
Alex K

Reputation: 7217

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..

Upvotes: 1

Views: 4619

Answers (3)

piotrm
piotrm

Reputation: 12356

You can use negative lookahead to match "&" not followed by "amp;".

$str = preg_replace( "/&(?!amp;)/", "&",  $str  );

Upvotes: 9

guiman
guiman

Reputation: 1334

Mmm i think that the best way to solve this is using the php function htmlspecialchars, here its the manual page . Basically you will need to disable doble_encode (put a false on it) so that already existing html entities are not parsed again.

Upvotes: 0

Platinum Azure
Platinum Azure

Reputation: 46183

Just do two substitutions:

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

Upvotes: 16

Related Questions