免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1488 | 回复: 0
打印 上一主题 下一主题

[心得] 函数trim() 的妙用~ [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-11-06 21:49 |只看该作者 |倒序浏览
    一直再用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:

  • " " (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"]?>
怎么样这个函数是不是很实用呢!~


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/10559/showart_55505.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP