开发者

url parsing in php - turning spaces to pluses

is there a php function for doing something like this: turning this - 701 First Ave, Sunnyvale, CA to this - 701+First+Ave,+Sunnyvale,+CA thanks开发者_StackOverflow中文版.


If you only want to replace spaces:

$result = str_replace(" ", "+", $source);

Documentation for str_replace

If you also want to replace other characters that would interfere on the URL (like ?, &, etc)

$result = urlencode($source);

Documentation for urlencode


str_replace(" ", "+", $address); should do the trick.


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

This is especially designed to encode string to used in URL. There is a mirror function for decoding back as well, just check the doc.


Yes, there is: urlencode.


function replaceSpaceByPlus($string) {
  return str_replace(" ", "+", $string);
}

and use echo replaceSpaceByPlus($string); where string it the tekst you want to have replaced (or return, or whatever you want to do with the value.


Why do you need it? Maybe you're searching for urlencode()?

Otherwise, for a simple replace go for str_replace(), but if you need to url-encode a string, than you should really go for urlencode() which is more fit, because replaces all the symbols that are not allowed in a URL.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜