Home  >  Article  >  Backend Development  >  PHP removes spaces and trim is invalid, use str_replace to implement_PHP tutorial

PHP removes spaces and trim is invalid, use str_replace to implement_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:45:01887browse

PHP has the related function trime to remove spaces. It can remove two spaces or directly use the ltrim and rtrim functions. The results are the same as trim. If you want to delete all spaces, you can only use str_replace.

Usually, I use trim to filter out spaces. Today I found out that the spaces in the middle cannot be removed. Unfortunately, after checking the instructions, I found out that trim can only remove spaces at both ends, such as $abc = a b c;, and using trim can only remove spaces. The spaces before a and after c are removed, but what about the spaces before and after b. str_replace comes into play, and that’s it.

Students who need to remove spaces, please be careful. PHP removes spaces, trim does not work, str_replace does, but trim can delete the leading spaces as follows
The code is as follows
 代码如下 复制代码

str_replace(' ','',$abc).

Copy code

str_replace(' ','',$abc).

 代码如下 复制代码

trim 去除一个字符串两端空格,
rtrim 是去除一个字符串右部空格,
ltrim 是去除一个字符串左部空格。

echo trim(" 空格www.bKjia.c0m ")."
";
echo rtrim(" 空格 ")."
";
echo ltrim(" 空格 ")."
";
?>

Example
The code is as follows

Copy code
trim removes spaces at both ends of a string,
rtrim is to remove spaces on the right side of a string,
ltrim removes spaces from the left of a string. echo trim("space www.bKjia.c0m ")."
";
echo rtrim("space ")."
";
echo ltrim("space ")."
";
?> The effect achieved is the same as str_replace
http://www.bkjia.com/PHPjc/633056.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/633056.htmlTechArticlephp has a related function trime to remove spaces. It can remove two spaces or directly use the ltrim and rtrim function results and trim. Same, if you want to delete all spaces, you can only use str_replace. ...
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