preg_ replace替换&;不符合有趣的性格


preg_replace replacing &not in string to funny character

由于某种原因,当preg_replace在字符串中看到&not并将其替换为¬:时

$url= "http://something?blah=2&you=3&rate=22&nothing=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&nothing=1   // Expected result

你知道为什么会发生这种情况以及如何预防吗?

&在使用URI时具有特殊意义。您的URI包含&not,它本身就是一个有效的HTML实体。它正在被转换为¬,因此引起了问题。将它们作为&amp;not适当地转义以避免此问题。如果您的数据是从其他地方获取的,则可以使用htmlspecialchars()自动执行此操作。

使用此&amp;代替这个&

因为您的&no具有特殊意义

使用这个网址:

http://something?blah=2&amp;you=3&amp;rate=22&amp;nothing=1

然后进行相应的替换