範例、先清除字串前後的空白字符再移除字串間空白
<?php
$TestStr = " This string have many whitespace.";
echo '<pre>'.$TestStr.'<br></pre>';
$TestStr = trim($TestStr);
echo '<pre>'.$TestStr.'<br></pre>';
$TestStr = preg_replace('/\s(?=)/', '', $TestStr);
echo '<pre>'.$TestStr.'</pre>';
?>
輸出結果$TestStr = " This string have many whitespace.";
echo '<pre>'.$TestStr.'<br></pre>';
$TestStr = trim($TestStr);
echo '<pre>'.$TestStr.'<br></pre>';
$TestStr = preg_replace('/\s(?=)/', '', $TestStr);
echo '<pre>'.$TestStr.'</pre>';
?>
This string have many whitespace.
This string have many whitespace.
Thisstringhavemanywhitespace.
延伸閱讀