萬盛學電腦網

 萬盛學電腦網 >> 數據庫 >> mysql教程 >> PHP和MySQL的刪除空白函數介紹

PHP和MySQL的刪除空白函數介紹

作為黃金搭檔,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.
"" (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.

copyright © 萬盛學電腦網 all rights reserved