How to remove leading and trailing spaces in php

藏色散人
Release: 2023-03-08 14:24:01
Original
2280 people have browsed it

在php中,可以通过trim()函数来去掉首尾空格;该函数的作用是移除字符串两侧的空白字符或其他预定义字符,其使用语法是“trim(string,charlist)”,参数string规定要检查的字符串,参数charlist规定从字符串中删除哪些字符,如果省略该参数,则可移除空格、回车、换行、垂直制表符等预定义字符。

How to remove leading and trailing spaces in php

本文操作环境:Windows7系统、PHP8、Dell G3电脑。

php去掉首尾空格:

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

相关函数:

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

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

语法

trim(string,charlist)
Copy after login

参数

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

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

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

实例

移除字符串两侧的空格:

<?php
$str = " Hello World! ";
echo "Without trim: " . $str;
echo "<br>";
echo "With trim: " . trim($str);
?>
Copy after login

How to remove leading and trailing spaces in php

【推荐:《PHP视频教程》】

The above is the detailed content of How to remove leading and trailing 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