Home > Article > Backend Development > How to remove spaces in php string
How to remove spaces from strings in php: 1. Use the trim function to remove spaces at both ends of the string; 2. Use ltrim to remove only the first spaces in the string; 3. Use the rtrim function to convert to remove spaces.
Recommended: "PHP Video Tutorial"
You can use trim to remove spaces at the beginning and end of the string. The following characters There is a space at the beginning and end of the string "my name is haha".
After trim conversion, the spaces at both ends are removed.
You can use ltrim to remove only the first space in the string. The following string "my name is haha" has a space in the first part.
After conversion through ltrim, the leading spaces are removed, but the trailing spaces are still there.
You can use ltrim to remove only the trailing spaces in the string. There is a trailing space in the following string "my name is haha".
After conversion through rtrim, the trailing spaces are removed, but the leading spaces are still there.
Notes
Spaces often cause program logic processing problems and often need to be removed
The above is the detailed content of How to remove spaces in php string. For more information, please follow other related articles on the PHP Chinese website!