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

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

PHP中文网
Release: 2023-03-01 06:50:01
Original
1592 people have browsed it

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
Copy after login

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template