What does trim in php mean?

藏色散人
Release: 2023-03-14 17:18:01
Original
4138 people have browsed it

php的trim是一种内置函数,用来去除字符串首尾处的空白字符或者其他字符,一般用在服务端对接收的用户数据进行处理,以免把用户误输入的空格存储到数据库。

What does trim in php mean?

本文操作环境:windows7系统、PHP7.1版、DELL G3电脑

php的trim是什么意思?

PHP trim()函数的作用和使用方法

PHP trim()函数一般是用来去除字符串首尾处的空白字符(或者其他字符),一般在用在服务端对接收的用户数据进行处理,以免把用户误输入的空格存储到数据库,下次对比数据时候出错。

该函数有两个参数,第二个可以为空,格式如下:

trim ( string $str [, string $character_mask = ” \t\n\r\0\x0B” ] );
Copy after login

$str 为需要处理的字符串,如果$character_mask不为空,只会清楚$character_mask上写的要求清楚的字符;如果第二个参数$character_mask为空,会过滤以下字符:

” ” (ASCII 32 (0x20)),普通空格符。

“\t” (ASCII 9 (0x09)),制表符。

“\n” (ASCII 10 (0x0A)),换行符。

“\r” (ASCII 13 (0x0D)),回车符。

“\0” (ASCII 0 (0x00)),空字节符。

“\x0B” (ASCII 11 (0x0B)),垂直制表符。

数据处理后,返回的是过滤后的字符串。

设置一个参数$str:

<?php
$data = "我有空格 ";
$data2 = ";";
echo $data.$data2;
echo "<br>";
echo trim($data).$data2;
echo "<br>";
Copy after login

很显然,trim函数处理后,字符串data的空格被去掉了。

我们加设置第二个参数看下效果:

<?php
$data = "我有空格 ";
$data2 = ";";
echo $data.$data2;
echo "<br>";
echo trim($data,&#39;\t&#39;).$data2;//指定删除制表符号\t
echo "<br>";
Copy after login

很显然,我们的data没有制表符,所以即便trim处理过了,我们的空格还是存在。

推荐学习:《PHP视频教程

The above is the detailed content of What does trim in php mean?. 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