PHP字符串函数之 trim ltrim rtrim chop
来源:互联网 发布:易友进销存软件 编辑:程序博客网 时间:2024/06/08 03:49
PHP字符串函数之 trim ltrim rtrim chop
- trim – 去除字符串首尾处的空白字符(或者其他字符)
- ltrim – 删除字符串开头的空白字符(或其他字符)
- rtrim – 删除字符串末端的空白字符(或者其他字符)
- chop – rtrim() 的别名
trim
去除字符串首尾处的空白字符(或者其他字符)
string trim ( string $str [, string $charlist = " \t\n\r\0\x0B" ] )
参数说明
str
待处理的字符串。
charlist
可选参数,过滤字符也可由 charlist 参数指定。一般要列出所有希望过滤的字符,也可以使用 “..” 列出一个字符范围。
返回值
过滤后的字符串。
注意
- 该函数区分大小写
- 此函数返回字符串 str 去除首尾空白字符后的结果。如果不指定第二个参数,trim() 将去除这些字符:
" " (ASCII 32 (0x20)),普通空格符。
"\t" (ASCII 9 (0x09)),制表符。
"\n" (ASCII 10 (0x0A)),换行符。
"\r" (ASCII 13 (0x0D)),回车符。
"\0" (ASCII 0 (0x00)),空字节符。
"\x0B" (ASCII 11 (0x0B)),垂直制表符。
示例
<?php/* 去空格 */$text = " There are a few words ";var_dump($text); //string ' There are a few words ' (length=23)var_dump(trim($text)); //string 'There are a few words' (length=21)/* 去制表符 */$text = "\tThere are a few words\t";var_dump($text); //string ' There are a few words ' (length=25)var_dump(trim($text)); //string 'There are a few words' (length=21)/* 去换行符 */$text = "\nThere are a few words\n";var_dump($text); /*string 'There are a few words' (length=23) */var_dump(trim($text)); //string 'There are a few words' (length=21)/* 去回车符 */$text = "\rThere are a few words\r";var_dump($text); /*string 'There are a few words' (length=23) */var_dump(trim($text)); //string 'There are a few words' (length=21)/* 去空字节符 */$text = "\0There are a few words\0";var_dump($text); //string '�There are a few words�' (length=23)var_dump(trim($text));//string 'There are a few words' (length=21)/* 去垂直制表符 */$text = "\x0BThere are a few words\x0B";var_dump($text); //string ' There are a few words ' (length=23)var_dump(trim($text));//string 'There are a few words' (length=21)/* 各种去 */$text = "\x0B\r\n\t\0 There are a few words \x0B\r\n\t\0";var_dump($text);/*string ' � There are a few words �' (length=33)*/var_dump(trim($text)); //string 'There are a few words' (length=21)?>
<?php/* 去掉指定的字符 */$text = "There are a few words";$text = trim($text, 's');var_dump($text); //string 'There are a few word' (length=20)/* 去掉指定的字符串 */$text = "There are a few words";$text = trim($text, 'words');var_dump($text); //string 'There are a few ' (length=16)/* 第二个参数为数字是无效的 */$text = "There are a few words";$text = trim($text, 115);var_dump($text); //string 'There are a few words' (length=21)/* 第二个参数为ASCII码 (s的ASCII为 115(0x73)) */$text = "There are a few words";$text = trim($text, "\x73");var_dump($text); //string 'There are a few word' (length=20)/* 去掉 0-31 ASCII控制字符 */$text = "\x01\x02There are a few words\x0A\x1F";$text = trim($text, "\x00..\x1F");var_dump($text); //string 'There are a few words' (length=21)?>
ltrim
删除字符串开头的空白字符(或其他字符)
string ltrim ( string $str [, string $character_mask ] )
该函数与trim的区别是仅仅去除左侧的空白字符。示例可以参考trim
参数说明
str
待处理的字符串。
character_mask
可选参数,过滤字符也可由 character_mask 参数指定。一般要列出所有希望过滤的字符,也可以使用 “..” 列出一个字符范围。
返回值
过滤后的字符串。
注意
- 该函数区分大小写
- 该函数返回一个删除了 str 最左边的空白字符的字符串。 如果不使用第二个参数, ltrim() 仅删除以下字符:
" " (ASCII 32 (0x20)),普通空格符。
"\t" (ASCII 9 (0x09)),制表符。
"\n" (ASCII 10 (0x0A)),换行符。
"\r" (ASCII 13 (0x0D)),回车符。
"\0" (ASCII 0 (0x00)),空字节符。
"\x0B" (ASCII 11 (0x0B)),垂直制表符。
示例
<?php$text = "There are a few words";$text = ltrim($text, "T");var_dump($text); //string 'here are a few words' (length=20)$text = "There are a few words";$text = ltrim($text, "s"); var_dump($text); //string 'There are a few words' (length=21)?>
rtrim
删除字符串末端的空白字符(或者其他字符)
string rtrim ( string $str [, string $character_mask ] )
该函数与trim的区别是仅仅去除右侧的空白字符。示例可以参考trim
参数说明
str
待处理的字符串。
character_mask
可选参数,过滤字符也可由 character_mask 参数指定。一般要列出所有希望过滤的字符,也可以使用 “..” 列出一个字符范围。
返回值
过滤后的字符串。
注意
- 该函数区分大小写
- 该函数返回一个删除了 str 最右边的空白字符的字符串。 如果不使用第二个参数, rtrim() 仅删除以下字符:
" " (ASCII 32 (0x20)),普通空格符。
"\t" (ASCII 9 (0x09)),制表符。
"\n" (ASCII 10 (0x0A)),换行符。
"\r" (ASCII 13 (0x0D)),回车符。
"\0" (ASCII 0 (0x00)),空字节符。
"\x0B" (ASCII 11 (0x0B)),垂直制表符。
示例
<?php$text = "There are a few words";$text = rtrim($text, "T");var_dump($text); //string 'There are a few words' (length=21)$text = "There are a few words";$text = rtrim($text, "s"); var_dump($text); //string 'There are a few word' (length=20)?>
chop
删除字符串末端的空白字符(或者其他字符)
string chop ( string $str [, string $character_mask ] )
该函数是 rtrim()
的别名。可参考rtrim()
参数说明
str
待处理的字符串。
character_mask
可选参数,过滤字符也可由 character_mask 参数指定。一般要列出所有希望过滤的字符,也可以使用 “..” 列出一个字符范围。
返回值
过滤后的字符串。
注意
- 该函数区分大小写
- 该函数返回一个删除了 str 最右边的空白字符的字符串。 如果不使用第二个参数, chop() 仅删除以下字符:
" " (ASCII 32 (0x20)),普通空格符。
"\t" (ASCII 9 (0x09)),制表符。
"\n" (ASCII 10 (0x0A)),换行符。
"\r" (ASCII 13 (0x0D)),回车符。
"\0" (ASCII 0 (0x00)),空字节符。
"\x0B" (ASCII 11 (0x0B)),垂直制表符。
示例
<?php$text = "There are a few words";$text = chop($text, "T");var_dump($text); //string 'There are a few words' (length=21)$text = "There are a few words";$text = chop($text, "s"); var_dump($text); //string 'There are a few word' (length=20)?>
1 0
- PHP字符串函数之 trim ltrim rtrim chop
- PHP字符串操作之trim/rtrim/ltrim
- PHP trim ltrim rtrim
- ORACLE字符串截取函数trim(),ltrim(),rtrim()
- ORACLE字符串截取函数trim(),ltrim(),rtrim()
- LTrim、RTrim 和 Trim 函数
- LTrim、RTrim 和 Trim 函数
- LTrim、RTrim 和 Trim 函数
- LTrim、RTrim 和 Trim 函数
- Oracle ltrim rtrim trim函数
- 字符串删除函数 ltrim() 和 rtrim() 和 trim()
- PHP中trim/rtrim/ltrim的用法
- php ltrim() rtrim() trim()删除字符空格
- js的trim(),Ltrim(),Rtrim()函数
- 为JavaScript添加trim,ltrim,rtrim函数
- js 的Trim、LTrim、RTrim函数
- js trim,ltrim,rtrim函数使用方法
- javascript的trim,ltrim,rtrim自定义函数
- UITableView-FDTemplateLayoutCell 使用
- 谈谈程序中BSS段大小问题
- leetcode--Palindrome Number
- [Android4.4]NetworkManagementService与Netd交互流程
- 混淆矩阵
- PHP字符串函数之 trim ltrim rtrim chop
- Hadoop伪分布式
- ider 注册
- continue
- 优先队列——二项队列(binominal queue)
- BW关于主数据和文本抽数的问题
- HTML5 LocalStorage 本地存储
- break 语句
- Usb Audio Device Descriptor(9) Standard Audio Streaming Isochronous Audio Data Endpoint Descriptor