Home > Article > Backend Development > 2 ways to remove leading and trailing spaces in PHP_PHP Tutorial
This article mainly introduces two ways to remove leading and trailing spaces in PHP. This article gives the preg_replace substitution and trim functions. Two methods and examples are given. Friends in need can refer to it
This seems to be a very simple problem, but it is actually a bit tricky. First of all, the spaces are escaped here, not strings, and cannot be removed directly using trim().
1, replace
with preg_replaceThe code is as follows:
//The results are as follows:
string 'dfadad On the relationship between the responsible people and the three British 775fd' (length=35)
2, trim method
The code is as follows:
//The results are as follows:
string 'dfadad 3333adf775fd' (length=19)