Home > php教程 > php手册 > body text

php去空格trim无效用str_replace实现

WBOY
Release: 2016-06-13 09:48:06
Original
1227 people have browsed it

php去空格有相关函数 trime,他可以去除两个空格或直接使用ltrim与rtrim函数结果与trim一样,如果要删除所有空格只有使用str_replace替换了。

平时过虑空格,一般都是用trim,今天发现,中间的空格去不掉.倒,查了说明才知道,trim只能去两头的,比如$abc =  a b c; ,用trim只能把a前面c后面的空格去掉,但是b前后的空格怎么办呢. str_replace出场了,这样就行了

 代码如下 复制代码

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

需要去空格的同学们小心了,php去空格,trim不行,str_replace行了,但是trim可以删除头空格了如下

 代码如下 复制代码

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

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

达到的效果与str_replace是一样的

source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!