In PHP to delete the function is more specific than JS, in addition to the trim () function, there are LTrim () and RTrim () function, they have to delete the left and right space, in addition to these three functions can also use regular delete.
LTrim () function
LTrim ($str, $charlist)
$STR represents the string being processed;
$charlist is the special character to be removed, and if empty, the left-hand space is removed
The code is as follows |
Copy Code |
$t = "... I ' m Jacky ... "; echo "a". $t. " "; $left =ltrim ($t); echo "a". $left. " "; $lleft =ltrim ($left, "."); Echo $lleft; ?> |
RTrim () function
RTrim ($str, $charlist)
$STR represents the string being processed;
$charlist is the special character to be removed, and if empty, the right end of the space is removed
The code is as follows |
Copy Code |
$a = "htm"; echo $a. " L "." "; echo RTrim ($a). " L "; ?> |
Trim () function
First, turn the trailing blanks.
The code is as follows |
Copy Code |
$STR = "This line containstliberal RN use of whitespace.nn"; First, turn the trailing blanks. $str = Trim ($STR); Then remove more than two spaces above the $str = Preg_replace ('/s (? =s)/', ' ', $str);
Finally, replace the non-whitespace with a space $str = preg_replace ('/[nrt]/', ' ', $str); |
Use the example above to remove all the extra spaces. First Use trim () to remove the trailing space, and then use Preg_replace () to eliminate the repeated spaces
More powerful by replacing regular expressions
PHP removes string and trailing spaces (including full-width)
The code is as follows |
Copy Code |
$STR = "One-piece tutorial network www.bkjia.com"; $str = Mb_ereg_replace (' ^ (|) + ', ', $str); $str = Mb_ereg_replace (' (|) +$ ', ', $str); Echo mb_ereg_replace (', ' n ', $str); ?> |
http://www.bkjia.com/PHPjc/445628.html www.bkjia.com true http://www.bkjia.com/PHPjc/445628.html techarticle in PHP to delete the function is more specific than JS, in addition to the trim () function, there are LTrim () and RTrim () function, they have to delete the left and right space, in addition to these three functions can also use positive ...