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

What is the php function to remove spaces?

coldplay.xixi
Release: 2023-03-07 08:08:01
Original
3311 people have browsed it

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前面的空白就没有了
Copy after login

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

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

2、rtrim函数

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

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

3、trim函数

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

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

实例应用

<?php
$str = &#39;Demon is an English teacher.17847564092&#39;;
$str = rtrim($str,&#39;0..9&#39;);
echo $str;
//删除末尾多余的数字
Copy after login

疑问:这里为什么不用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!

Related labels:
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template