Home >Backend Development >PHP Problem >What to do if php trim() cannot remove spaces

What to do if php trim() cannot remove spaces

coldplay.xixi
coldplay.xixiOriginal
2020-08-18 09:46:263897browse

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)].

What to do if php trim() cannot remove spaces

Solution to the problem that php trim() cannot remove spaces:

php’s trim() is the one we use most String space removal function, but trim() 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn