用户名:
密 码: 记住
您当前的位置:首页 > 网络编程 > mysql教程

PHP和MySQL的删除空白函数介绍

时间:2015-01-23  来源:西部数据  作者:西部数据

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

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

以下是引用片段:

PHP为:Strip whitespace (or other characters) 

MySQL为:space characters removed

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

对“other characters”,手册解释为,以下是引用片段:

  1. " " (ASCII 32 (0x20)), an ordinary space.  
  2. "t" (ASCII 9 (0x09)), a tab.  
  3. "n" (ASCII 10 (0x0A)), a new line (line feed).  
  4. "r" (ASCII 13 (0x0D)), a carriage return.  
  5. "" (ASCII 0 (0x00)), the NUL-byte.  
  6. "x0B" (ASCII 11 (0x0B)), a vertical tab.  

不过,MySQL的trim()函数也增加了对其他字符的删除,具体请见下面从手册上摘抄的解释.

以下为MySQL的函数解释:

LTRIM(str),Returns the string str with leading space characters removed.

以下是代码片段:

  1. mysql> SELECT LTRIM('  barbar'); 
  2.         -> 'barbar' 
  3.  
  4.  
  5.  
  6.     This function is multi-byte safe. 
  7.  
  8.     RTRIM(str) 
  9.  
  10.     Returns the string str with trailing space characters removed. 

以下是代码片段:

  1. mysql> SELECT RTRIM('barbar   '); 
  2.         -> 'barbar' 
  3.  
  4.     This function is multi-byte safe. 
  5.  
  6.     TRIM([{BOTH | LEADING | TRAILING} [remstr] FROM] str), TRIM([remstr FROM] str) 
  7.  
  8.     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. 

以下是代码片段,代码如下:

  1. mysql> SELECT TRIM('  bar   '); 
  2.         -> 'bar' 
  3. mysql> SELECT TRIM(LEADING 'x' FROM 'xxxbarxxx'); 
  4.         -> 'barxxx' 
  5. mysql> SELECT TRIM(BOTH 'x' FROM 'xxxbarxxx'); 
  6.         -> 'bar' 
  7. mysql> SELECT TRIM(TRAILING 'xyz' FROM 'barxxyz'); 
  8.         -> 'barx'  --phpfensi.com 
  9.  
  10.     This function is multi-byte safe.
来顶一下
返回首页
返回首页
推荐资讯
WiFi太不安全:7岁女孩11分钟内入侵公共网络 WiFi太不安全:7岁女孩11分钟内入侵近期刚刚发布研究说WiFi网络能获得人们手机里多少私人信息,
不服跑个分?人工智能也出现“刷分”乱象 不服跑个分?人工智能也出现“刷分2014年,人工智能领域突然爆发,成为了科研和科技创业的热门
相关文章
    无相关信息
栏目更新
栏目热门