由于某种原因,当preg_replace
在字符串中看到¬
并将其替换为¬
:时
$url= "http://something?blah=2&you=3&rate=22¬hing=1";
echo preg_replace("/&rate=[0-9]*/", "", $url) . "<br/>";
但输出如下:
http://something?blah=2&you=3¬hing=1 // Current result
http://something?blah=2&you=3¬hing=1 // Expected result
你知道为什么会发生这种情况以及如何预防吗?
&
在使用URI时具有特殊意义。您的URI包含¬
,它本身就是一个有效的HTML实体。它正在被转换为¬
,因此引起了问题。将它们作为&not
适当地转义以避免此问题。如果您的数据是从其他地方获取的,则可以使用htmlspecialchars()
自动执行此操作。
使用此&
;代替这个&
因为您的&no
具有特殊意义
使用这个网址:
http://something?blah=2&you=3&rate=22&nothing=1
然后进行相应的替换