Home > Article > Backend Development > How to remove full-width spaces in php
## The operating environment of this article: Windows 7 system, PHP version 7.1 , Dell G3 computer. How to remove full-width spaces in php? php common methods for removing empty characters and spaces, full-width spaces, line breaks, etc. in stringsHow to remove full-width spaces in php: First create a PHP sample file; then use the "function myTrim($str){...}" method to remove full-width spaces.
ltrim() – Removes null characters from the front of the string
rtrim() – Removes null characters from the end of the string
chop() – Same as 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>"; ?>Output results
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 Video Tutorial"
The above is the detailed content of How to remove full-width spaces in php. For more information, please follow other related articles on the PHP Chinese website!