【发布时间】:2016-08-05 11:19:18
【问题描述】:
我不知何故无法让它工作: 我有一个简单的字符串,例如:
<p>Foo & Bar</p> // <-- this should still be &
<a href="http://test.com/?php=true&test=test&p=p"> // <- This string should only be affected and be changed to &
<div> Yes ü No</div> // <-- This should still be ü
<a href="http://mycoolpage.com/?page=1&amp;fun=true&amp;foo=bar&amp;yes=no">
现在我想用&amp; 替换所有&amp; 和preg_replace,我试图为此创建一个正则表达式,但不知何故我无法让它工作。
这就是我已经走了多远,它只找到最后一个 &amp; 并且还匹配它之前的整个字符串并且找不到另一个。我做错了什么?
(?>=href\=\").*?(&amp;)(?=\")
编辑:不能使用 htmlentities_decode 或 htmlspecialchars_decode,因为还有其他代码会受到影响。
【问题讨论】:
-
试过
html_entity_decode? ideone.com/6DyYuQ -
或者更难的方式:ideone.com/ADku3b
-
我会试试
while(regex match) { str replace } -
请再次检查问题,我添加了一些内容。
标签: php regex preg-replace