Search code examples
phpdrupal-6str-replace

PHP - str_replace not working as expected


I have a variable called path that is a url e.g.

www.google.co.uk%3Fq%3Dde

which i am running through 2 str_replace to format properly.

  $path = str_replace('%3F', '?', $path); 
  $path = str_replace ('%3D', '=', $path);

I am then printing the output but the "%3F" and "%3D" are still being printed. This did previously work when i used these 2 str_replace

  $path = str_replace('%3F', '?'); 
  $path = str_replace ('%3D', '=');

But drupal 6 throws up errors when i use these.

I am quite new to php so it may be something simple that i am missing


Solution

  • Don't use str_replace() for this. Use urldecode() instead, as this is its designed purpose.

    echo urldecode("www.google.co.uk%3Fq%3Dde");
    // www.google.co.uk?q=de
    

    Without seeing the rest of your code, it is difficult to say why your first two str_replace() calls failed. They are correctly formed.