位置: 编程技术 - 正文

PHP字符串函数trim()的用法(php字符串操作函数)

编辑:rootadmin
trim

推荐整理分享PHP字符串函数trim()的用法(php字符串操作函数),希望有所帮助,仅作参考,欢迎阅读内容。

文章相关热门搜索词:php string函数,在php中,字符串有哪些表示形式,php字符串函数大全,php字符串变量,php字符串变量,php字符串赋值,php字符串赋值,php字符串的三种定义方式,内容如对您有帮助,希望把文章链接给更多的朋友!

(PHP 4, PHP 5)

trim — 去除字符串首尾处的空白字符(或者其他字符)

说明 string trim ( string $str [, string $charlist = " tnrx0B" ] )

此函数返回字符串 str 去除首尾空白字符后的结果。如果不指定第二个参数,trim() 将去除这些字符: " " (ASCII (0x)),普通空格符。 "t" (ASCII 9 (0x)),制表符。 "n" (ASCII (0x0A)),换行符。 "r" (ASCII (0x0D)),回车符。 "" (ASCII 0 (0x)),空字节符。 "x0B" (ASCII (0x0B)),垂直制表符。

参数

str

待处理的字符串。

charlist

可选参数,过滤字符也可由 charlist 参数指定。一般要列出所有希望过滤的字符,也可以使用 “..” 列出一个字符范围。

返回值

过滤后的字符串。

更新日志

版本 说明 4.1.0 新增可选的 charlist 参数。

范例 PHP字符串函数trim()的用法(php字符串操作函数)

Example #1 trim() 使用范例

<?php$text="ttTheseareafewwords:)...";$binary="xExamplestringx0A";$hello="HelloWorld";var_dump($text,$binary,$hello);print"n";$trimmed=trim($text);var_dump($trimmed);$trimmed=trim($text,"t.");var_dump($trimmed);$trimmed=trim($hello,"Hdle");var_dump($trimmed);//清除$binary首位的ASCII控制字符//(包括0-)$clean=trim($binary,"x..x1F");var_dump($clean);?>

以上例程会输出:

Example #2 使用 trim() 清理数组值

<?phpfunctiontrim_value(&$value){$value=trim($value);}$fruit=array('apple','banana','cranberry');var_dump($fruit);array_walk($fruit,'trim_value');var_dump($fruit);?>

以上例程会输出:

注释

Note: Possible gotcha: removing middle characters

Because trim() trims characters from the beginning and end of a string, it may be confusing when characters are (or are not) removed from the middle. trim(&#;abc&#;, &#;bad&#;) removes both &#;a&#; and &#;b&#; because it trims &#;a&#; thus moving &#;b&#; to the beginning to also be trimmed. So, this is why it "works" whereas trim(&#;abc&#;, &#;b&#;) seemingly does not.

参见

ltrim() - 删除字符串开头的空白字符(或其他字符) rtrim() - 删除字符串末端的空白字符(或者其他字符) str_replace() - 子字符串替换

PHP字符串函数substr_count()的用法 substr_count(PHP4,PHP5)substr_count计算字串出现的次数说明intsubstr_count(string$haystack,string$needle[,int$offset=0[,int$length]])substr_count()返回子字符串needle在字符串haystack

PHP字符串函数substr_compare()的用法 substr_compare(PHP5)substr_compare二进制安全比较字符串(从偏移位置比较指定长度)说明intsubstr_compare(string$main_str,string$str,int$offset[,int$length[,bool$case_insensitivit

PHP字符串函数strtoupper()的用法 strtoupper(PHP4,PHP5)strtoupper将字符串转化为大写说明stringstrtoupper(string$string)将string中所有的字母字符转换为大写并返回。注意字母与当前所在区域有关。例

本文链接地址:https://www.jiuchutong.com/biancheng/278798.html 转载请保留说明!

上一篇:PHP字符串函数substr_replace()的用法(php字符串型数据的定义方式)

下一篇:PHP字符串函数substr_count()的用法(php 字符串 数组)

免责声明:网站部分图片文字素材来源于网络,如有侵权,请及时告知,我们会第一时间删除,谢谢! 邮箱:opceo@qq.com

鄂ICP备2023003026号

网站地图: 企业信息 工商信息 财税知识 网络常识 编程技术

友情链接: 武汉网站建设 电脑维修 湖南楚通运网络