Home > Backend Development > PHP Problem > How to remove preceding spaces in php

How to remove preceding spaces in php

藏色散人
Release: 2023-03-08 19:04:02
Original
1867 people have browsed it

在php中,可以通过ltrim函数实现去除字符串前面空格;该函数的作用就是移除字符串左侧的空白字符或其他预定义字符,其语法是“ltrim(string,charlist)”;参数charlist规定从字符串中删除哪些字符,如果省略该参数则会移除一些预定义字符。

How to remove preceding spaces in php

本文操作环境:windows7系统、PHP8版,DELL G3电脑

ltrim() 函数移除字符串左侧的空白字符或其他预定义字符。

相关函数:

rtrim() - 移除字符串右侧的空白字符或其他预定义字符

trim() - 移除字符串两侧的空白字符或其他预定义字符

语法

ltrim(string,charlist)
Copy after login

参数

string:必需。规定要检查的字符串。

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

"\0" - NULL
"\t" - 制表符
"\n" - 换行
"\x0B" - 垂直制表符
"\r" - 回车
" " - 空格
Copy after login

例子

移除字符串左侧的空格:

<?php
$str = "    Hello World!";
echo "不使用 ltrim: " . $str;
echo "<br>";
echo "使用 ltrim: " . ltrim($str);
?>
Copy after login

以上代码的浏览器输出:

不使用 ltrim: Hello World!
使用 ltrim: Hello World!
Copy after login

推荐学习:《PHP视频教程

The above is the detailed content of How to remove preceding spaces in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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