Home > Article > Backend Development > How to remove spaces from string in PHP
PHP removes spaces in the string. We can use the PHP trim() function to achieve this.
We can use the PHP trim() function to remove spaces containing non-breaking spaces, newlines, and tabs from the beginning and end of a string. But it does not remove the spaces appearing in the middle of the string.
The code picture is as follows:
The output result is:
3328
You can also copy the following code and test it locally:
<?php $my_str = ' Welcome to Tutorial Republic '; echo strlen($my_str); // Outputs: 33 $trimmed_str = trim($my_str); echo strlen($trimmed_str); // Outputs: 28
This article is a brief introduction to removing spaces in strings in PHP.
The above is the detailed content of How to remove spaces from string in PHP. For more information, please follow other related articles on the PHP Chinese website!