This article illustrates the way PHP deletes left and right-end spaces. Share to everyone for your reference. The specific methods are as follows:
In PHP to delete a function than JS more specific, in addition to the trim () function, there are LTrim () and RTrim () function, they want to delete the space around, in addition to these three functions can also be used to delete.
LTrim () function: LTrim ($STR, $charlist)
The $STR represents the string being processed, $charlist is the special character to be removed, and if it is empty, the left side of the space is removed and the code is as follows:
Copy Code code 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)
The $STR represents the string being processed, $charlist is the special character to be deleted, and if it is empty, the right side of the space is removed and the code is as follows:
Copy Code code as follows:
<?php
$a = "htm";
echo $a. " L "." <br> ";
echo RTrim ($a). " L ";
?>
trim () function, first go to the tail space, the code is as follows:
Copy Code code as follows:
$STR = "This line containstliberal RN with Whitespace.nn";
First go to the tail space
$str = Trim ($STR);
Then remove the two spaces above the
$str = Preg_replace ('/s (? =s)/', ', $str);
Finally, replace the non-space with a space
$str = preg_replace ('/[nrt]/', ', ', $str);
Use the example above to remove all the extra spaces, first using trim () to the ends of the space, and then using Preg_replace () to remove the duplicate spaces.
with regular expression replacement , more powerful, PHP to remove the end of the string space (including the full corner), the code is as follows:
Copy Code code as follows:
<?php
$STR = "cloud-dwelling community www.jb51.net";
$str = Mb_ereg_replace (' ^ (|) + ', ', $str);
$str = Mb_ereg_replace (' (|) +$ ', ', $str);
Echo Mb_ereg_replace ("," N ", $str);
?>
I hope this article will help you with your PHP program design.