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

A simple guide to using the trim() function in PHP, a guide to using trim_PHP tutorial

WBOY
Release: 2016-07-13 09:56:50
Original
944 people have browsed it

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

string trim ( string $str [, string $charlist ] ) - Remove whitespace characters at the beginning and end of a string (or other characters)

When the second parameter is empty, the trim() function will remove spaces, tabs, line feeds, carriage returns, vertical tabs, etc. by default. When the second parameter is added

Copy code The code is as follows:
  1) trim(' "string"', '"sg'); // Final output: "strin
  2) trim(' "string" ', '"sg'); // Final output: "string"
  2)trim('"string"', '"sg');    // Final output: trin

Therefore, 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. It is also applicable to the ltrim() and rtrim() functions

Definition and usage

The PHP function trim() removes whitespace characters and other predefined characters from both ends of a string.

Grammar

trim(str,charlist) Parameter 1 str is the string to be operated, parameter 2 charlist is optional and specifies the special symbols to be removed.

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.
" "x0B" (ASCII 11 (0x0B)), a vertical tab.
If you want to remove other characters, you can set it in the second parameter.

Example 1

Output:
<&#63;php 
$str = " 使用函数trim去掉字符串两端空白字符 "; 
$str1 = trim($str); 
echo "处理前有".strlen($str)."个字符"; echo "<br/>"; 
//www.phpjc.cn echo "<br/>"; 
echo "使用trim函数处理后有".strlen($str1)."个字符"; 
&#63;> 
Copy after login

There are 39 characters before processing and there are 34 characters after processing using PHP function trim()

Example 2

Output:
<&#63;php 
$str = "##使用函数trim去掉字符串两端特定字符####"; 
$str1 = trim($str,"#"); 
//为函数trim传入第二个参数,
trim将删除字符串$str两端的#字符 echo $str."<br>"; 
echo $str1; 
&#63;> 
Copy after login

##Use the PHP function trim() to remove specific characters at both ends of the string#### Use the function trim() to remove specific characters at both ends of the string

The above is the entire content of this article, I hope you all like it.

http://www.bkjia.com/PHPjc/985279.html

truehttp: //www.bkjia.com/PHPjc/985279.htmlTechArticleA simple guide to using the trim() function in PHP, a guide to using trim string trim ( string $str [, string $charlist ] ) - Remove blank characters (or other characters) at the beginning and end of the string trim() function...
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