在php字符串中删除所有不必要的空格

faq*_*faq 3 php str-replace

我想删除所有不在两个字母之间的空格:

$string = "              bah    bah    bah bah  ";
$string = str_replace("/w /w", "+", $string);
// what goes here? to get this:
$string = "bah+bah+bah+bah"; 
Run Code Online (Sandbox Code Playgroud)

我的想法是,我想摆脱所有不必要的空间(不仅在开头和结尾).这不是一个链接,但对于一个搜索框,提交时会爆炸,所以+甚至可以是=或任何基本的东西

cas*_*raf 7

$string = preg_replace('/\s+/', ' ', $string);
Run Code Online (Sandbox Code Playgroud)