Question

For some reason when preg_replace sees &not in string and replaces it with ¬:

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

echo preg_replace("/&rate=[0-9]*/", "", $url) . "<br/>";

But the output is as follows:

http://something?blah=2&you=3¬hing=1      // Current result
http://something?blah=2&you=3&nothing=1   // Expected result

Any ideas why this is happening and how to prevent it?

Was it helpful?

Solution

& has special meaning when used URIs. Your URI contains &not, which is a valid HTML entity on its own. It's being converted to ¬, hence causing the trouble. Escape them properly as &amp;not to avoid this problem. If your data is fetched from elsewhere, you can use htmlspecialchars() to do this automatically.

OTHER TIPS

Use this &amp; in place of this &

because your &no has special meaning

use this url :

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

and then do your replace accordingly

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top