PHP和MySQL的删除空白函数

来源:芽雨快跑  时间: 2008-08-05 10:41:22  浏览: 23522 次  评论: 0 篇  
Tags : MySQL PHP trim  

    作为黄金搭档,PHP和MySQL都有自己的删除空白函数,而且函数名字也一样:trim(), ltrim(), rtrim()。当然,作为编程语言,PHP删除空白函数更为强大。

    对 ltrim()和rtrim(),从其英语解释来看:

以下是引用片段:
PHP为:Strip whitespace (or other characters)
MySQL为:space characters removed

    显然,PHP还可以有“other characters”,而且PHP的函数还可以用第二个参数自定义要删除的字符。

    对“other characters”,手册解释为:

以下是引用片段:
  • " " (ASCII 32 (0x20)), an ordinary space.
  • "\t" (ASCII 9 (0x09)), a tab.
  • "\n" (ASCII 10 (0x0A)), a new line (line feed).
  • "\r" (ASCII 13 (0x0D)), a carriage return.
  • "\0" (ASCII 0 (0x00)), the NUL-byte.
  • "\x0B" (ASCII 11 (0x0B)), a vertical tab.
  •     不过,MySQL的trim()函数也增加了对其他字符的删除。具体请见下面从手册上摘抄的解释。

    = = = = = = = = = = = = = 方便阅读的分隔线  = = = = = = = = = = = = = = = =

        以下为MySQL的函数解释

        LTRIM(str) 

        Returns the string str with leading space characters removed.

    以下是代码片段:
    mysql> SELECT LTRIM('  barbar');
            -> 'barbar'


        This function is multi-byte safe.

        RTRIM(str)

        Returns the string str with trailing space characters removed.

    以下是代码片段:
    mysql> SELECT RTRIM('barbar   ');
            -> 'barbar'

        This function is multi-byte safe.

        TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str), TRIM([remstr FROM] str)

        Returns the string str with all remstr prefixes or suffixes removed. If none of the specifiers BOTH, LEADING, or TRAILING is given, BOTH is assumed. remstr is optional and, if not specified, spaces are removed.

    以下是代码片段:
    mysql> SELECT TRIM('  bar   ');
            -> 'bar'
    mysql> SELECT TRIM(LEADING 'x' FROM 'xxxbarxxx');
            -> 'barxxx'
    mysql> SELECT TRIM(BOTH 'x' FROM 'xxxbarxxx');
            -> 'bar'
    mysql> SELECT TRIM(TRAILING 'xyz' FROM 'barxxyz');
            -> 'barx'

        This function is multi-byte safe.

        参考:
        php手册
        mysql中英文手册:
        http://dev.mysql.com/doc/refman/5.1/zh/functions.html
        http://dev.mysql.com/doc/refman/5.1/en/string-functions.html#function_trim

    也许您对下面的文章感兴趣:
    朋友的回复:
    要不您也来写一写?
    © 2004 - 2017 芽雨快跑 - 本页面所有内容,未经芽雨许可,欢迎转载,请注明出处

    京ICP备09017802号