How to remove leading and trailing spaces in php: 1. Remove spaces through the "trim", "rtrim" and "ltrim" functions that come with PHP; 2. Replace and remove spaces through regular expressions, the syntax is as follows "mb_ereg_replace('^( | ) ', '', $str);".
Recommended: "PHP Video Tutorial"
How to remove spaces at the beginning and end of a string in PHP
When I was doing PHP today, I needed to remove the spaces at the beginning and end of the string. Baidu found out that PHP comes with this function trim, which is really much more convenient. I would like to share it with you
The first method: through the function that comes with php
1 2 3 4 5 6 7 8 9 10 11 12 |
|
The second method: through regular expression replacement, the function is stronger
php removes the leading and trailing spaces in the string (including Full width)
The code is as follows:
1 2 3 4 5 6 |
|
The above is the detailed content of How to remove leading and trailing spaces in php. For more information, please follow other related articles on the PHP Chinese website!