Home>Article>Backend Development> PHP function trim() that removes whitespace characters or other predefined characters on both sides of a string

PHP function trim() that removes whitespace characters or other predefined characters on both sides of a string

黄舟
黄舟 Original
2017-11-06 13:46:51 2164browse

Example

Remove the characters on the left side ofString("He" in "Hello" and "d!" in "World"):

"; echo trim($str,"Hed!"); ?>

Definition and Usage

trim() function removes whitespace characters or other predefined characters on both sides of a string.

Related functions:

  • ltrim() - Removes whitespace characters or other predefined characters on the left side of a string.

  • rtrim() - Removes whitespace characters or other predefined characters on the right side of the string.

Syntax

trim(string,charlist)
##Technical details
Parameters Description
string Required. Specifies the string to check.
charlist Optional. Specifies which charactersare removed from the string. If this parameter is omitted, all the following characters are removed:
  • "\0" - NULL

  • ##"\t" - TAB
  • "\n" - Line feed
  • "\x0B" - Vertical tab character
  • "\ r" - Enter
  • " " - Space

Return value: Return the modified string. PHP Version: 4+ ##Update Log :
In PHP 4.1, the charlist parameter was added.

更多实例

实例 1

移除字符串两侧的空格:

"; echo "With trim: " . trim($str); ?>

上面代码的 HTML 输出如下(查看源代码):

   Without trim: Hello World! 
With trim: Hello World!

上面代码的浏览器输出如下:

Without trim: Hello World! With trim: Hello World!

实例 2

移除字符串两侧的换行符(\n):

"; echo "With trim: " . trim($str); ?>

上面代码的 HTML 输出如下(查看源代码):

   Without trim: Hello World! 
With trim: Hello World!

上面代码的浏览器输出如下:

Without trim: Hello World! With trim: Hello World!

例子

"; echo $str1; ?>

输出:

##使用PHP函数trim()去掉字符串两端特定字符#### 使用函数trim去掉字符串两端特定字符


The above is the detailed content of PHP function trim() that removes whitespace characters or other predefined characters on both sides of a string. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn