位置: 编程技术 - 正文
推荐整理分享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 参数。
范例
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中所有的字母字符转换为大写并返回。注意字母与当前所在区域有关。例
标签: php字符串操作函数
本文链接地址:https://www.jiuchutong.com/biancheng/278798.html 转载请保留说明!友情链接: 武汉网站建设