开发者

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 EXCEP开发者_高级运维T 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..


Just do two substitutions:

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


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

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜