开发者

preg-replace, xhtml strict, php, & ampersand error [duplicate]

开发者 https://www.devze.com 2023-02-27 07:40 出处:网络
This question already has answers here: RegEx to Reject Unescaped HTML Character (2 answers) Closed 2 years ago.
This question already has answers here: RegEx to Reject Unescaped HTML Character (2 answers) Closed 2 years ago.

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.

0

精彩评论

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