将当前页面网址中的“+”替换为“%2B”或空格

I have a URL generated via form submission with passed variables. I won’t go into detail but the ‘+’ in the URL breaks the system, so I would like to replace it with ‘%2B’.

However, every method I have tried so far has not worked. I have tried the various PHP URL decode methods and none of them work, since they replace other characters in the URL which again breaks it.


The URL looks like this:

www.domain.com/catalog/search.php?sort_by=relevance&some_filter%5B%5D=filter+term


I have also tried getting the current page URL and using str_ireplace to replace the ‘+’ characters with regular spaces or ‘%2B’, all to no avail.

I would appreciate any help!
Thanks

If you're using php, try urlencode() and urldecode()

http://www.php.net/manual/en/function.urlencode.php

It will save you the headache of replacing the characters (which are then replaced).

Here is a more complete example close to your use case.

<?php

$foo = str_replace('+','%2B', 'www.domain.com/catalog/search.php?sort_by=relevance&some_filter%5B%5D=filter+term');

echo urldecode($foo);