How to use php trim function?

藏色散人
Release: 2023-04-06 07:52:01
Original
4817 people have browsed it

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)].

How to use php trim function?

php trim function usage:

Syntax:

trim($string, $charlist)
Copy after login

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” - 回车
“ ” - 普通的空格
Copy after login

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

Output:

o World
Copy after login

Code example 2:

<?php 

$str = "  php中文网 "; 
  
echo trim($str); 
?>
Copy after login

Output:

php中文网
Copy after login

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!

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