function:<br />/****************************************<br /> strWhitespace( $s )<br /> 字串空白處理<br /> param:<br /> string s 字串<br /> return:<br /> string<br />****************************************/<br />function strWhitespace( $s )<br />{<br /> //移除前後空白字<br /> $s = trim($s);<br /> //移除重覆的空白<br /> $s = preg_replace('/\s(?=\s)/', '', $s);<br /> //移除非空白的間距變成一般的空白<br /> $s = preg_replace('/[\n\r\t]/', ' ', $s);<br /> <br /> return $s;<br />}<br />測試:<br />$str = " This line contains\tliberal \r\n use of whitespace.\n\n";<br />echo "處理前︰<pre>{$str}</pre>";<br />$str = strWhitespace( $str );<br />echo "處理後︰<pre>{$str}</pre>";<br />輸出結果:<br />處理前︰<br /><br /> This line contains liberal <br /> use of whitespace.<br /><br /><br />處理後︰<br /><br />This line contains liberal use of whitespace.<br />
張貼留言
沒有留言:
張貼留言