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视频教程》
Atas ialah kandungan terperinci php怎么去掉全角空格. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!