php的trim是一种内置函数,用来去除字符串首尾处的空白字符或者其他字符,一般用在服务端对接收的用户数据进行处理,以免把用户误输入的空格存储到数据库。
本文操作环境:windows7系统、PHP7.1版、DELL G3电脑
php的trim是什么意思?
PHP trim()函数的作用和使用方法
PHP trim()函数一般是用来去除字符串首尾处的空白字符(或者其他字符),一般在用在服务端对接收的用户数据进行处理,以免把用户误输入的空格存储到数据库,下次对比数据时候出错。
该函数有两个参数,第二个可以为空,格式如下:
trim ( string $str [, string $character_mask = ” \t\n\r\0\x0B” ] );
$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>";
很显然,trim函数处理后,字符串data的空格被去掉了。
我们加设置第二个参数看下效果:
<?php $data = "我有空格 "; $data2 = ";"; echo $data.$data2; echo "<br>"; echo trim($data,'\t').$data2;//指定删除制表符号\t echo "<br>";
很显然,我们的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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.
