Home  >  Article  >  Backend Development  >  What is the php function to remove spaces?

What is the php function to remove spaces?

coldplay.xixi
coldplay.xixiOriginal
2020-11-04 10:15:103107browse

php删除空格函数是:1、ltrim函数,实现删除字符串开始位置的空格或其他字符;2、rtrim函数,实现删除字符串结束位置的空格或者其他字符;3、trim函数,实现删除字符串开始和结束位置的空格或者其他字符。

What is the php function to remove spaces?

php删除空格函数是:

1、ltrim函数

描述:实现删除字符串开始位置的空格或其他字符

语法:string ltrim(string $str [,string $charlist])

说明:charlist规定从字符串中删除哪些字符。如果缺省该参数,则移除下列所有字符

What is the php function to remove spaces?

$str = "\\n\\n\\t\\tABC\\t\\t";
echo ltrim($str);
//输出后,ABC前面的空白就没有了

如果我只想删除制表符,而不想删除换行,那么我们加参数↓

$str = "\\n\\n\\t\\tABC\\t\\t";
echo ltrim($str,"\\t");

2、rtrim函数

描述:实现删除字符串结束位置的空格或者其他字符

语法:string rtrim(string $str[,string $charlist])

3、trim函数

描述:实现删除字符串开始和结束位置的空格或者其他字符

语法:string trim(string $str[,string $charlist])

实例应用

疑问:这里为什么不用trim或者ltrim

猜想:从右侧开始遍历,如果遇到不符合参数条件的,函数停止执行。我们如果从左边开始,那么先碰到的是英文字母,不符合参数。

相关免费学习推荐:php编程(视频)

The above is the detailed content of What is the php function to remove spaces?. 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