Home >Backend Development >PHP Tutorial >How to use php trim function?
The trim function in PHP is a built-in function, which is used to remove spaces and predefined characters on the left and right sides of a string. Its syntax is [trim($string, $charlist)].
php trim function usage:
Syntax:
trim($string, $charlist)
Parameters:
The trim function accepts one mandatory parameter and one optional parameter, as shown in the following syntax.
#$string: This parameter specifies the string from which left and right whitespace and predefined characters are to be removed
$charlist: This is a Optional parameter specifying the characters to be removed from the string. If this is not mentioned, all the following characters will be removed:
“\ 0” - NULL “\ t” - 标签 “\ n” - 新行 “\ x0B” - 垂直制表符 "\ r” - 回车 “ ” - 普通的空格
Return value:
Returns by removing spaces and predefined characters on the left and right sides of the string Modified string.
Code example 1:
<?php $str = "Hello World!"; echo trim($str, "Hell!"); ?>
Output:
o World
Code example 2:
<?php $str = " php中文网 "; echo trim($str); ?>
Output:
php中文网
Related recommendations: "PHP Tutorial"
The above is the detailed content of How to use php trim function?. For more information, please follow other related articles on the PHP Chinese website!