[心得] 函数trim() 的妙用~
时间:2005-11-06 来源:Petery
一直再用PHP编写网站,呵呵~本人对PHP的编写也就是初级阶段,喜欢计算机,喜欢网络,喜欢编程,喜欢游戏,人纭:多而不精。此乃随笔小抄,请勿见笑~
不论你数据库用的是MySql还是普通的文本文件,在存取的时候转义字符的处理都要格外小心。
"
" windows下文本文件的换行符 "
"是Linux下的换行符。
如果在存字符串时丢了这些转义字符,那么肯定格式会出错的;在读取完字符串后,可能有时会出现莫名的换行,这时你就需要用上 trim() 函数了。
函数原型:string [color="#ffffff"]trim ( string str [, string charlist]) 注: The optional charlist parameter was added in PHP 4.1.0
This function returns a string with whitespace stripped from the beginning and end of str. Without the second parameter, [color="#ffffff"]trim() will strip these characters:
You can also specify the characters you want to strip, by means of the charlist parameter. Simply list all characters that you want to be stripped. With .. you can specify a range of characters.
例子 1. Usage example of [color="#ffffff"]trim()
[color="#000000"][color="#007700"];
[color="#0000bb"]$trimmed [color="#007700"]= [color="#ffffff"]trim[color="#007700"]([color="#0000bb"]$text[color="#007700"]);
[color="#ff8000"]// $trimmed = "These are a few words :) ..."
[color="#0000bb"]$trimmed [color="#007700"]= [color="#ffffff"]trim[color="#007700"]([color="#0000bb"]$text[color="#007700"], [color="#dd0000"]" ."[color="#007700"]);
[color="#ff8000"]// $trimmed = "These are a few words :)"
[color="#0000bb"]$clean [color="#007700"]= [color="#ffffff"]trim[color="#007700"]([color="#0000bb"]$binary[color="#007700"], [color="#dd0000"]"x00..x1F"[color="#007700"]);
[color="#ff8000"]// trim the ASCII control characters at the beginning and end of $binary
// (from 0 to 31 inclusive)
[color="#0000bb"]?>
怎么样这个函数是不是很实用呢!~
不论你数据库用的是MySql还是普通的文本文件,在存取的时候转义字符的处理都要格外小心。
"
" windows下文本文件的换行符 "
"是Linux下的换行符。
如果在存字符串时丢了这些转义字符,那么肯定格式会出错的;在读取完字符串后,可能有时会出现莫名的换行,这时你就需要用上 trim() 函数了。
函数原型:string [color="#ffffff"]trim ( string str [, string charlist]) 注: The optional charlist parameter was added in PHP 4.1.0
This function returns a string with whitespace stripped from the beginning and end of str. Without the second parameter, [color="#ffffff"]trim() will strip these characters:
-
" " (ASCII 32 (0x20)), an ordinary space.
-
" " (ASCII 9 (0x09)), a tab.
-
"
" (ASCII 10 (0x0A)), a new line (line feed).
-
"" (ASCII 13 (0x0D)), a carriage return.
-
"" (ASCII 0 (0x00)), the NUL-byte.
-
"x0B" (ASCII 11 (0x0B)), a vertical tab.
You can also specify the characters you want to strip, by means of the charlist parameter. Simply list all characters that you want to be stripped. With .. you can specify a range of characters.
例子 1. Usage example of [color="#ffffff"]trim()
[color="#000000"][color="#007700"];
[color="#0000bb"]$trimmed [color="#007700"]= [color="#ffffff"]trim[color="#007700"]([color="#0000bb"]$text[color="#007700"]);
[color="#ff8000"]// $trimmed = "These are a few words :) ..."
[color="#0000bb"]$trimmed [color="#007700"]= [color="#ffffff"]trim[color="#007700"]([color="#0000bb"]$text[color="#007700"], [color="#dd0000"]" ."[color="#007700"]);
[color="#ff8000"]// $trimmed = "These are a few words :)"
[color="#0000bb"]$clean [color="#007700"]= [color="#ffffff"]trim[color="#007700"]([color="#0000bb"]$binary[color="#007700"], [color="#dd0000"]"x00..x1F"[color="#007700"]);
[color="#ff8000"]// trim the ASCII control characters at the beginning and end of $binary
// (from 0 to 31 inclusive)
[color="#0000bb"]?>
怎么样这个函数是不是很实用呢!~
相关阅读 更多 +