Home  >  Article  >  Daily Programming  >  How to filter spaces in string in PHP

How to filter spaces in string in PHP

藏色散人
藏色散人Original
2018-12-06 16:47:369629browse

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.

How to filter spaces in string 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 = &#39; Hello PHP中文网 &#39;;
echo "原字符串的长度:".strlen($my_str);
echo "<br>";

Here we can output the original length of this string:

How to filter spaces in string in PHP

Filter space operation, the code is as follows:

<?php
$trimmed_str = trim($my_str);
echo "过滤后字符串的长度:". strlen($trimmed_str);

The result is as follows:

How to filter spaces in string in PHP

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!

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