Home > Backend Development > PHP Tutorial > How to use trim function in PHP

How to use trim function in PHP

青灯夜游
Release: 2023-04-06 13:06:02
Original
22173 people have browsed it

Usage of trim function in PHP: [trim()] function is a built-in function in PHP. It is used to remove spaces or predefined characters on the left and right sides of a string and return the modified string. Function The syntax is: [trim($string, $charlist)].

How to use trim function in PHP

The trim() function is a built-in function in PHP that can be used to remove spaces and predefined characters on the left and right sides of a string. The following article will introduce you to the trim() function in PHP and introduce how to use the trim() function. I hope it will be helpful to you.

PHP trim function

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

Related functions:

● ltrim() function: removes blank characters or other predefined characters on the left side of the string.

● rtrim() function: Remove blank characters or other predefined characters on the right side of the string.

Basic syntax

trim($string, $charlist)
Copy after login

Parameters: trim() function accepts one required parameter and one optional parameter

$string: Used to specify a string from which whitespace and left and right predefined characters are to be removed; required parameter and cannot be omitted.

$charlist: used to specify the characters to be removed from the string. Optional parameter, which can be omitted; if omitted, all the following characters will be deleted:

"\0" - NULL

"\t ” - Tag

“\n”-New line

“\x0B”-Vertical tab character

 ● “\r” - Enter

● “” - Ordinary white space

Return value: It returns a modified string by removing spaces as well as predefined characters on both sides of the left and right string.

Usage example of PHP trim function

Example 1:

<?php 
$str = "Hello World!"; 
echo trim($str, "Hell!"); 
?>
Copy after login

Output :

o World
Copy after login

Example 2:

<?php 
header("content-type:text/html;charset=utf-8"); 
$str = "  欢迎来到php中文网!  "; 
echo trim($str); 
?>
Copy after login

Output:

欢迎来到php中文网!
Copy after login

Related video tutorial recommendation: "PHP Tutorial"

The above is the detailed content of How to use trim function in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template