php去掉全角空格的方法:先建立一個PHP範例檔;然後透過「function myTrim($str){...}」方法去掉全角空格即可。
#本文操作環境:windows7系統、PHP7.1版、Dell G3電腦。
php怎麼去掉全角空格?
php移除字串中空字元和空格、全角空格、換行等的常用方法整理
使用正規preg_replace函數
去除字串空格的函數
trim() – 移除字串兩端的空字元
ltrim() – 移除字串前端的空字元
rtrim() – 移除字串末端的空字元
chop() –同rtrim( ).
<?php $text = " www.qipa250.com "; $leftTrimmed = ltrim($text); $rightTrimmed = rtrim($text); $bothTrimmed = trim($text); print("leftTrimmed = ($leftTrimmed)"); echo "<hr>"; print("rightTrimmed = ($rightTrimmed)"); echo "<hr>"; print("bothTrimmed = ($bothTrimmed)"); echo "<hr>"; ?>
輸出結果
function myTrim($str) { $search = array(" "," ","\n","\r","\t"); $replace = array("","","","",""); return str_replace($search, $replace, $str); } echo '当前字符串内容:'.$str = 'www.qipa250.com QIPA250 奇葩天地网 qipa250.com'; echo "<hr>"; echo '过滤后的字符串内容:'.myTrim($str); echo "<hr>";
結果如下:
echo preg_replace('# #', '', 'ab ab'); //输出 "abab"
以上是php怎麼去掉全角空格的詳細內容。更多資訊請關注PHP中文網其他相關文章!