Home  >  Article  >  Backend Development  >  A simple guide to using the trim() function in PHP

A simple guide to using the trim() function in PHP

PHP中文网
PHP中文网Original
2017-06-09 13:49:001577browse

PHP function trim() deletes blank characters and other predefined characters from both ends of a string. The specific syntax is trim(str, charlist). We will explain it in detail with code examples

string trim ( string $str [, string $charlist ] ) - Remove whitespace characters (or other characters) at the beginning and end of the string

  The trim() function, when the second parameter is empty, will remove spaces, tabs, line feeds, and carriage returns by default , vertical tab characters, etc. When adding the second parameter

the code is as follows:

    1) trim('   \"string\"', '\"sg');   // 最终输出:\"strin
    2) trim('   \"string\"   ', '\"sg');   // 最终输出:\"string\"
    2)trim('\"string\"', '\"sg');    // 最终输出:trin

So the trim() function first removes the blank characters at the beginning and end of the characters, and then filters out the given characters (list) to be removed, also Applicable to ltrim(), rtrim() functions

Definition and usage

PHP function trim() removes whitespace characters and other predefined characters from both ends of a string.

Syntax

trim(str,charlist) Parameter 1 str is the string to be operated on, parameter 2 charlist is optional and specifies the special symbols you want to remove.

If the second parameter is not given a value, the following characters will be removed by default:

" " (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.
"

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