preg-replace, xhtml strict, php, & ampersand error [duplicate]
Wondering if there is a handy preg_replace or similar function that will change all my &
to &
but will not change &
to &
(won't change an &
that is already in the proper format).
I am having problem displaying a page that has (user submitted) &
and I wanted to change them (from a block of text fetched from mysql) just before displaying in the page.
Help will be appreciated. Cheers.
PS: I thought of using a str_replace with an & with space on either side, but then I sto开发者_如何学Cpped thinking about typos like Bob &Jenny
in lieu of Bob & Jenny
(since user submitted).
You can use an ?!
assertion for that:
$html = preg_replace('/&(?!#?\w+;)/', "&", $html);
# or just (?!amp;)
This also skips existing & and other < or { entities. But it's certainly not faultless.
精彩评论