PHP字符串函数之 trim ltrim rtrim chop


  • trim – 去除字符串首尾处的空白字符(或者其他字符)
  • ltrim – 删除字符串开头的空白字符(或其他字符)
  • rtrim – 删除字符串末端的空白字符(或者其他字符)
  • chop – rtrim() 的别名

trim

去除字符串首尾处的空白字符(或者其他字符)

string trim ( string $str [, string $charlist = " \t\n\r\0\x0B" ] )

参数说明

str
待处理的字符串。
charlist
可选参数,过滤字符也可由 charlist 参数指定。一般要列出所有希望过滤的字符,也可以使用 “..” 列出一个字符范围。

返回值

过滤后的字符串。

注意

  1. 该函数区分大小写
  2. 此函数返回字符串 str 去除首尾空白字符后的结果。如果不指定第二个参数,trim() 将去除这些字符:
    " " (ASCII 32 (0x20)),普通空格符。
    "\t" (ASCII 9 (0x09)),制表符。
    "\n" (ASCII 10 (0x0A)),换行符。
    "\r" (ASCII 13 (0x0D)),回车符。
    "\0" (ASCII 0 (0x00)),空字节符。
    "\x0B" (ASCII 11 (0x0B)),垂直制表符。

示例

<?php
/* 去空格 */
$text = " There are a few words ";
var_dump($text);       //string ' There are a few words ' (length=23)
var_dump(trim($text)); //string 'There are a few words' (length=21)/* 去制表符 */
$text = "\tThere are a few words\t";
var_dump($text);       //string '   There are a few words   ' (length=25)
var_dump(trim($text)); //string 'There are a few words' (length=21)/* 去换行符 */
$text = "\nThere are a few words\n";
var_dump($text);
/*string '
There are a few words
' (length=23) */
var_dump(trim($text)); //string 'There are a few words' (length=21)/* 去回车符 */
$text = "\rThere are a few words\r";
var_dump($text);
/*string '
There are a few words
' (length=23) */
var_dump(trim($text)); //string 'There are a few words' (length=21)/* 去空字节符 */
$text = "\0There are a few words\0";
var_dump($text);      //string 'There are a few words' (length=23)
var_dump(trim($text));//string 'There are a few words' (length=21)/* 去垂直制表符 */
$text = "\x0BThere are a few words\x0B";
var_dump($text);      //string ' There are a few words ' (length=23)
var_dump(trim($text));//string 'There are a few words' (length=21)/* 各种去 */
$text = "\x0B\r\n\t\0 There are a few words \x0B\r\n\t\0";
var_dump($text);
/*string ' There are a few words     ' (length=33)*/
var_dump(trim($text)); //string 'There are a few words' (length=21)
?>
<?php
/* 去掉指定的字符 */
$text = "There are a few words";
$text = trim($text, 's');
var_dump($text); //string 'There are a few word' (length=20)/* 去掉指定的字符串 */
$text = "There are a few words";
$text = trim($text, 'words');
var_dump($text); //string 'There are a few ' (length=16)/* 第二个参数为数字是无效的 */
$text = "There are a few words";
$text = trim($text, 115);
var_dump($text); //string 'There are a few words' (length=21)/* 第二个参数为ASCII码 (s的ASCII为 115(0x73))  */
$text = "There are a few words";
$text = trim($text, "\x73");
var_dump($text); //string 'There are a few word' (length=20)/* 去掉 0-31 ASCII控制字符 */
$text = "\x01\x02There are a few words\x0A\x1F";
$text = trim($text, "\x00..\x1F");
var_dump($text); //string 'There are a few words' (length=21)
?>

ltrim

删除字符串开头的空白字符(或其他字符)

string ltrim ( string $str [, string $character_mask ] )

该函数与trim的区别是仅仅去除左侧的空白字符。示例可以参考trim

参数说明

str
待处理的字符串。
character_mask
可选参数,过滤字符也可由 character_mask 参数指定。一般要列出所有希望过滤的字符,也可以使用 “..” 列出一个字符范围。

返回值

过滤后的字符串。

注意

  1. 该函数区分大小写
  2. 该函数返回一个删除了 str 最左边的空白字符的字符串。 如果不使用第二个参数, ltrim() 仅删除以下字符:
    " " (ASCII 32 (0x20)),普通空格符。
    "\t" (ASCII 9 (0x09)),制表符。
    "\n" (ASCII 10 (0x0A)),换行符。
    "\r" (ASCII 13 (0x0D)),回车符。
    "\0" (ASCII 0 (0x00)),空字节符。
    "\x0B" (ASCII 11 (0x0B)),垂直制表符。

示例

<?php
$text = "There are a few words";
$text = ltrim($text, "T");
var_dump($text); //string 'here are a few words' (length=20)$text = "There are a few words";
$text = ltrim($text, "s");
var_dump($text); //string 'There are a few words' (length=21)
?>

rtrim

删除字符串末端的空白字符(或者其他字符)

string rtrim ( string $str [, string $character_mask ] )

该函数与trim的区别是仅仅去除右侧的空白字符。示例可以参考trim

参数说明

str
待处理的字符串。
character_mask
可选参数,过滤字符也可由 character_mask 参数指定。一般要列出所有希望过滤的字符,也可以使用 “..” 列出一个字符范围。

返回值

过滤后的字符串。

注意

  1. 该函数区分大小写
  2. 该函数返回一个删除了 str 最右边的空白字符的字符串。 如果不使用第二个参数, rtrim() 仅删除以下字符:
    " " (ASCII 32 (0x20)),普通空格符。
    "\t" (ASCII 9 (0x09)),制表符。
    "\n" (ASCII 10 (0x0A)),换行符。
    "\r" (ASCII 13 (0x0D)),回车符。
    "\0" (ASCII 0 (0x00)),空字节符。
    "\x0B" (ASCII 11 (0x0B)),垂直制表符。

示例

<?php
$text = "There are a few words";
$text = rtrim($text, "T");
var_dump($text); //string 'There are a few words' (length=21)$text = "There are a few words";
$text = rtrim($text, "s");
var_dump($text); //string 'There are a few word' (length=20)
?>

chop

删除字符串末端的空白字符(或者其他字符)

string chop ( string $str [, string $character_mask ] )

该函数是 rtrim() 的别名。可参考rtrim()

参数说明

str
待处理的字符串。
character_mask
可选参数,过滤字符也可由 character_mask 参数指定。一般要列出所有希望过滤的字符,也可以使用 “..” 列出一个字符范围。

返回值

过滤后的字符串。

注意

  1. 该函数区分大小写
  2. 该函数返回一个删除了 str 最右边的空白字符的字符串。 如果不使用第二个参数, chop() 仅删除以下字符:
    " " (ASCII 32 (0x20)),普通空格符。
    "\t" (ASCII 9 (0x09)),制表符。
    "\n" (ASCII 10 (0x0A)),换行符。
    "\r" (ASCII 13 (0x0D)),回车符。
    "\0" (ASCII 0 (0x00)),空字节符。
    "\x0B" (ASCII 11 (0x0B)),垂直制表符。

示例

<?php
$text = "There are a few words";
$text = chop($text, "T");
var_dump($text); //string 'There are a few words' (length=21)$text = "There are a few words";
$text = chop($text, "s");
var_dump($text); //string 'There are a few word' (length=20)
?>

PHP字符串函数之 trim ltrim rtrim chop相关推荐

  1. [导入]JavaScript常用函数:Trim() LTrim() RTrim()

    //功能:JavaScript的Trim(), Ltrim(), RTrim() 函数 //来源:http://jorkin.reallydo.com/article.asp?id=460 Strin ...

  2. Oracle之trim,ltrim,rtrim三个函数的用法

    基础用法: 去除指定字符串前后的空格 select trim(' hello caicai ')trim from dual;--当不加别名时,显示字符串右边还是有空格. 2.去除指定字符串左右的空格 ...

  3. C语言的Trim, LTrim, RTrim

    抽空写了个C语言的Trim(), LTrim(), RTrim(),代码如下: #include <iostream> using namespace std; char * LTrim( ...

  4. php之 trim ltrim rtrim 小坑

    trim, ltrim, rtrim , 有第二个参数,可以指定过滤的字符 小坑demo: //ltrim区分左边的 间隔就是右边第一个字符, 这样写没有可读性 $str = "qwerty ...

  5. PHP字符串去除首尾指定字符的trim ltrim rtrim函数

    今天,我们看一个比较简单的处理字符串函数,我们都知道在表单提交的过程中,可用户输入的内容不一定就是和你想的一样合法的数据,就比如输入用户名或者邮箱的时候在input框空了几个空格,这样进入数据库中的数 ...

  6. 数据库学习之MySQL (九)—— 数学函数 字符串函数 CONCAT TRIM PAD

    MySQL学习专栏 正在持续更新中:) 文章目录 复习一下 前面学到的函数 LENGTH CONCAT 数学函数 ABS CEIL FLOOR ROUND POW 常用字符串函数 SUBSTR REP ...

  7. C语言去除字符串空格的方法ltrim/rtrim/strim

    自定义一个C语言去除字符串左边空格,去除字符串右边空格,去除字符串两边空格的方法.代码如下: #include <stdio.h> #include <stdlib.h> #i ...

  8. ORACLE字符串截取函数trim(),ltrim(),rtrim()

    1.TRIM([[LEADING||TRAILING||BOTH] c2 FROM] c1). 看起来很复杂,理解起来很简单: (1)如果没有指定任何参数则oracle去除c1头尾空格 例如:SELE ...

  9. substr instr trim ltrim rtrim函数详解

    substr('源字符串',开始位置,截取长度)        substr('This is a test', 6, 2)     would return 'is'        substr(' ...

最新文章

  1. 计算机网络 关于网速,关于电脑网速慢的说明
  2. R语言na.omit函数删除NA值实战
  3. 编码区和非编码区的关系
  4. 【ELK】ELK集群搭建(ElasticSearch Logstash Kinaba)
  5. OMG: daily scrum six
  6. VARIANT变体类型数据
  7. python 读写文件 另存为_python读写文件(五)
  8. egg 编码规范_从 Egg.js 到 NestJS,爱码客后端选型之路
  9. log4j.properties配置与将异常输出到Log日志文件实例
  10. linux命令应用大词典.pdf,Linux命令应用大词典
  11. 实例学习ZMODEM文件传输协议
  12. 探讨破解3G今日困局之策
  13. 我对锤子ROM 功能的看法——功能篇
  14. 成立两只产业基金,微盟如何布局SaaS生态?
  15. 怎样让自己的【微信公众号】快速涨粉?
  16. 曝!苹果折叠iPhone要问世了
  17. Android 逆向基础
  18. Ubuntu安装搜狗输入法后修改默认英文输入状态的方法
  19. vue循环jq渲染网页页面
  20. GridView分页详解

热门文章

  1. matlab如何模拟转子断条,转子断条故障案例分析
  2. 李国杰:云计算不可忽视计算机系统研究
  3. box2d升级至 Box2DFlash 2.1a
  4. [Linux MySQL] MySQL 不停服务来启用 innodb_file_per_table
  5. 网友传秘方:解决新版SP1中BT下载慢问题
  6. 融优学堂生物演化12.5
  7. nodejs+vue+elementui线上读书会活动报名系统python/php/java
  8. 第十周—C语言 个人所得税的计算
  9. 数据安全之个人信息保存期限最小化的判定
  10. 【算法实战篇】时序多分类赛题-2020数字中国创新大赛-智慧海洋建设top5方案(含源码)