2011-07-20 35 views
1

我想剥离所有不在两个单词之间的空格吗?该脚本说明了一切:)str_replace php中的问题

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

的想法是,我想摆脱所有不必要的空格(不仅在开头和结尾)

+0

你会使用这个网址内部? – armandomiani

+0

它是一个搜索框 – faq

+0

我删除了空间后,我想爆炸它(但已经完成) – faq

回答

3

难道你们就不能只是修剪空格和使用urlencode()来将内部空间转换为+?如果你有其他字符不能被URL编码,这是行不通的。但我们不知道你的全部要求。

urlencode(trim($string)); 


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

// bah+bah 
+0

这就是我得到这个:“+++++++++++ bah + bah”..我正在寻找最简单的方法来得到这个:“bah + bah”; – faq

+0

@ zolee3003你确定你在'urlencode()'中包含'trim()'吗?这对我来说很好。 –

+0

我做了一个测试,它也可以工作。 – armandomiani

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

装饰()删除所有不必要的空格

4

trim将删除空格开头和结尾:

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