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

How to use trim function in PHP

青灯夜游
青灯夜游Original
2019-05-05 14:35:5721979browse

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)

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!"); 
?>

Output :

o World

Example 2:

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

Output:

欢迎来到php中文网!

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!

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