Home > Article > Daily Programming > How to filter spaces in string in PHP
PHP filtering string spaces is one of the common basic questions in the PHP interview process. It is also a basic PHP knowledge point that we need to master in our daily project development. PHP filtering string spaces is also very simple. You mainly need to understand the trim function in PHP.
Below we will introduce to you through a simple code example, How PHP filters spaces in strings.
The code for a string with spaces is as follows:
<?php $my_str = ' Hello PHP中文网 '; echo "原字符串的长度:".strlen($my_str); echo "<br>";
Here we can output the original length of this string:
Filter space operation, the code is as follows:
<?php $trimmed_str = trim($my_str); echo "过滤后字符串的长度:". strlen($trimmed_str);
The result is as follows:
Main function:
trim() function means removing blank characters or other predefined characters on both sides of the string.
Related functions:
ltrim() means removing blank characters or other predefined characters on the left side of the string.
rtrim() means removing whitespace characters or other predefined characters on the right side of the string.
This article is an introduction to how PHP filters spaces in strings. It is very simple and easy to understand. I hope it will be helpful to friends in need!
The above is the detailed content of How to filter spaces in string in PHP. For more information, please follow other related articles on the PHP Chinese website!