Home>Article>Backend Development> What to do if php trim() cannot remove spaces
Solution to the problem that php trim() cannot remove the spaces: Use regular expressions to remove the spaces at the beginning of the string, including full-width and half-width, the code is [$user = mb_ereg_replace('^( | ) ',' ', $name)].
Solution to the problem that php trim() cannot remove spaces:
php’s trim() is the one we use most String space removal function, buttrim()
cannot remove half-width spaces. At this time, you need to take other methods
This regular expression can remove the first space in the string , regardless of full-width or half-width
The code is as follows:
$user = mb_ereg_replace('^( | )+','', $name);
$user = mb_ereg_replace('( | )+$','', $name);
Related learning recommendations:php programming(video)
The above is the detailed content of What to do if php trim() cannot remove spaces. For more information, please follow other related articles on the PHP Chinese website!