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 and the code is as follows:
<?php
$t = "... I ' m Jacky ... ";
echo "a". $t. " <br> ";
$left =ltrim ($t);
echo "a". $left. " <br> ";
$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-hand space is removed, and the code is as follows:
<?php
$a = "htm";
echo $a. " L "." <br> ";
echo RTrim ($a). " L ";
?>
Trim () function, first to turn the trailing space, the code is as follows:
$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);
Using the above example, you can remove all the extra spaces, first use trim () to go to the leading and trailing blanks, and then use Preg_replace () to remove the repeated spaces.
By replacing the regular expression, the function is stronger, PHP removes the string from the end of the line (including the full width), the code is as follows:
<?php
$str = "PHP fan net www.phpfensi.com";
$str = Mb_ereg_replace (' ^ (|) + ', ', $str);
$str = Mb_ereg_replace (' (|) +$ ', ', $str);
Echo mb_ereg_replace (', ' n ', $str);
?>