How can I replace the "+" plus sign with its corresponding url encoding of "%2B"?
I'm having some trouble replacing the "+" sign with its urlencoded string of "%2B". How can I do this?
This is what I've tried:
Text Entered into text box:
plus(+)
I then urlencode the string:
$string = urlencode($string);
String now looks like:
plus%28+%29
I want to have the "+" urlencoded, or else when I urldecode() the data to display in browser I end up with:
plus( )
because urldecode() interprets the "+" to be a space.
I tried using php's str_replac开发者_如何学Ce() but I keep getting a "NULL" returned as the value for "$new_string":
$new_string = str_replace('+', '%2B', $string);
Any ideas?
Thanks in advance!
That is strange. When I use urlencode
on plus(+)
I get plus%28%2B%29
. Make sure you're using it correctly.
You might also try rawurlencode
. It will encode spaces as %20
instead of +
.
That helped me:
function _rawurlencode($string) {
$string = rawurlencode(str_replace('+','%2B',$string));
return $string;
}
精彩评论