开发者

str_replace problem in php

I want to strip all spaces 开发者_JAVA技巧that are not between two words? The script says it all:)

$string = "              bah bah    bah  ";
$string = str_replace("/w /w", "+", $string);
// i want string to look like this:
$string = "bah+bah+bah"; 

The idea is that i want to get rid of all unnecessary spaces(not only at the beginning and end)


trim will remove the whitespace at the beginning and end:

$string = trim($string);
echo str_replace(" ", "+", $string);


Can you not just trim whitespace and use urlencode() to convert the interior spaces to +? If you have other characters which cannot tolerate being url encoded, this will not work. But we don't know your full requirements.

urlencode(trim($string));


$string = "              bah bah";
echo urlencode(trim($string));

// bah+bah


$string = str_replace("/w /w", "+", trim($string));

trim() deletes all unnecessary spaces

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜