trim

  • (PHP 4, PHP 5, PHP 7)
  • trim — Strip whitespace (or other characters) from the beginning and end of a string
  • trim — 去除字符串首尾处的空白字符(或者其他字符)

Description

string trim ( string $str [, string $character_mask = " \t\n\r\0\x0B" ] )
//This function returns a string with whitespace stripped from the beginning and end of str. Without the second parameter, trim() will strip these characters:
//此函数返回字符串 str 去除首尾空白字符后的结果。如果不指定第二个参数,trim() 将去除这些字符: " " (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.垂直制表符。

Parameters

str

  • The string that will be trimmed.
  • 待处理的字符串。

character_mask

  • Optionally, the stripped characters can also be specified using the character_mask parameter. Simply list all characters that you want to be stripped. With .. you can specify a range of characters.
  • 可选参数,过滤字符也可由 character_mask 参数指定。一般要列出所有希望过滤的字符,也可以使用 “..” 列出一个字符范围。

Return Values

  • The trimmed string.
  • 过滤后的字符串。

Examples

<?php
/**
* Created by PhpStorm.
* User: zhangrongxiang
* Date: 2018/3/4
* Time: 下午3:10
*/
//去除字符串首尾处的空白字符(或者其他字符)
$hello = "Hello World";
//Hello World
echo $hello . PHP_EOL;
//Hello World
echo trim( $hello ) . PHP_EOL;
//o Wor
echo trim( $hello, "Hdle" ) . PHP_EOL;
//Hello Wor
echo trim( $hello, "dle" ) . PHP_EOL;
//llo World
echo trim( $hello, "He" ) . PHP_EOL;
//ello World
echo trim( $hello, "H" ) . PHP_EOL;
//Hello World
echo trim( $hello, "e" ) . PHP_EOL;
/**
* Note: Possible gotcha: removing middle characters
* Because trim() trims characters from the beginning and end of a string,
* it may be confusing when characters are (or are not) removed from the middle.
* trim('abc', 'bad') removes both 'a' and 'b' because it trims 'a' thus moving 'b' to the beginning to also be trimmed.
* So, this is why it "works" whereas trim('abc', 'b') seemingly does not.
*/
echo trim( 'abc', 'bad' ) . PHP_EOL;//c
echo trim( 'abc', 'a' ) . PHP_EOL;//bc $text = "\t\tThese are a few words :) ... ";
// These are a few words :) ...
echo $text . PHP_EOL;
//These are a few words :) ...
echo trim( $text ) . PHP_EOL; $binary = "\x09Example string\x0A";
// Example string
//
echo $binary . PHP_EOL;
//Example string
echo trim( $binary ) . PHP_EOL;
// 清除 $binary 首位的 ASCII 控制字符
// (包括 0-31)
//Example string
echo trim( $binary, "\x00..\x1F" ) . PHP_EOL; /////////////////////////////////////////////////////////////////////////////////////
function trim_value( &$value ) {
$value = trim( $value );
} $fruit = array( 'apple', 'banana ', ' cranberry ' );
//[0] => apple
//[1] => banana
//[2] => cranberry
print_r( $fruit ); array_walk( $fruit, 'trim_value' );
//[0] => apple
//[1] => banana
//[2] => cranberry
print_r( $fruit ); $path = '/Users/zhangrongxiang/WorkSpace/phpProjects/PHPTEST';
echo trim( $path ) . PHP_EOL;
// Users/zhangrongxiang/WorkSpace/phpProjects/PHPTEST
echo trim( $path, '/' ) . PHP_EOL;

See

All rights reserved

PHP之string之trim()函数使用的更多相关文章

  1. java中string.trim()函数的使用

    java中string.trim()函数的的作用是去掉字符串开头和结尾的空格,防止不必要的空格导致的错误. public static void main(String arg[]){ String ...

  2. js String Trim函数

    <javascript> String.prototype.trim = function() { return this.replace(/(^\s*)|(\s*$)/g,"& ...

  3. Delphi的Trim函数

    三个Trim函数简介 函数原型 function Trim(const S: string): string; 将字符串前后的空白及控制字元清掉. 注意Trim函数只能清掉字符串前后的空格及控制字元, ...

  4. PHP trim()函数的一些用法

    string trim ( string $str [, string $charlist ] ) - 去除字符串首尾处的空白字符(或者其他字符) trim()函数当第二个参数为空时,默认去掉空格.制 ...

  5. JQuery中trim函数的具体实现代码

    由于Javascript 1.8.1 之前的版本,没有内置 trim 函数,所以 JQuery 对它有自己的实现.不同的JQuery版本,trim函数的实现也不尽相同. 阅读本文需要掌握正则表达式用法 ...

  6. trim()函数IE7/8不兼容

    js中重写trim()函数 <script type="text/javascript">     String.prototype.trim = function() ...

  7. js判断字符串是否全为空(使用trim函数/正则表达式)

    我们需要判断用户输入的是否全是空格,可以使用以下方法: 方法一: 使用trim() /* 使用String.trim()函数,来判断字符串是否全为空*/ function kongge1(test) ...

  8. IE7、IE8不兼容js trim函数的解决方法

    IE兼容,有时候让人头疼,但又不得不去解决. 先看看一下代码: <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xh ...

  9. oracle中的trim()函数详解

    1.先看一下Oracle TRIM函数的完整语法描述 TRIM([ { { LEADING | TRAILING | BOTH }[ trim_character ]| trim_character} ...

随机推荐

  1. Android-XML与JSON的理解-JSON的数据格式

    据我了解,在多年以前浏览器客户端和服务器它们的通讯数据交互格式是XML, 使用XML来规定数据格式可读性确实非常强,XML的魅力确实很大,也很成熟,但是也有不足之处,就是在网络传输的时候对流量要求特别 ...

  2. 简单配置vps,防ddos攻击

    防人之心不可无. 网上总有些无聊或者有意的人.不多说了.上干货,配置vps apf防小流量ddos攻击. 对于大流量的ddos攻击, 需要机房的硬件防火墙,vps内部可能也扛不住. 1. 安装 DDo ...

  3. 安装git出现templates not found的问题

    背景 goods.api需要在新机器上部署,该机器上没有安装git,需要安装git,查询git版本为2.4.5-1.el6 ,使用yum 一顿安装后,执行git clone命令告知warning: t ...

  4. jQuery表单2

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. 记一次MBR锁机病毒分析

    有一天,在机缘巧合之下我获得了一个锁机软件(是多巧合阿喂!),然后兴高采烈的把它拖入了虚拟机里蹂躏(>_<!). 很巧,软件有虚拟机检测... Emmmm好吧,随便过一下... 我用的虚拟 ...

  6. PHP之编写日志文件留后门(免杀)

    (我知道你们都喜欢干货,所以也没亏待你们,请到文末吧,成果附件已上传~) 本文原创作者:Laimooc(原名xoanHn) 鄙人宗旨: 本人秉着爱学习爱恶搞爱研究爱进步并且遵纪守法的心态写下这篇文章, ...

  7. mongodb 3.0下载安装、配置及mongodb最新特性、基本命令教程详细介绍

    mongoDB简介(本文由www.169it.com搜集整理) MongoDB是一个高性能,开源,无模式的文档型数据库,是目前在IT行业非常流行的一种非关系型数据库(NoSql).它在许多场景下可用于 ...

  8. 使用Docker构建jdk1.8镜像

    一.下载centos镜像 下载自己需要的版本TAG,详见: docker安装指定版本TAG的镜像 $ sudo docker pull centos:centos7 二.下载jdk1.8,并上传到/u ...

  9. FlowPortal-BPM——移动手机端配置与IIS发布

    一.移动手机端配置 (1)VS打开文件夹iAnyWhere,配置config文件 (2)BPM-Web文件config中设置(设置为外网网址) 二.BPM设置 勾选移动审批可以设置要展示的字段信息,修 ...

  10. app.module.ts说明

    import { BrowserModule } from '@angular/platform-browser'; import { NgModule } from '@angular/core'; ...