Home  >  Article  >  Backend Development  >  A simple guide to using the trim() function in PHP_PHP Tutorial

A simple guide to using the trim() function in PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 09:57:06830browse

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

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

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

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

1

1

2

3

4

5

6

7

$str = " 使用函数trim去掉字符串两端空白字符 ";

$str1 = trim($str);

echo "处理前有".strlen($str)."个字符"; echo "
";

//www.phpjc.cn echo "
";

echo "使用trim函数处理后有".strlen($str1)."个字符";

?>

2 3

4

5

6

7

1

2

3

4

5

6

7

$str = "##使用函数trim去掉字符串两端特定字符####";

$str1 = trim($str,"#");

//为函数trim传入第二个参数,

trim将删除字符串$str两端的#字符 echo $str."
";

echo $str1;

?>

$str1 = trim($str);

echo "There are ".strlen($str)." characters before processing"; echo "
"; //www.phpjc.cn echo "
";

echo "After using the trim function, there are ".strlen($str1)." characters";?>
Output: There are 39 characters before processing and there are 34 characters after processing using PHP function trim() Example 2
1 2 3 4 5 6 7 <🎜>$str = "##Use function trim to remove specific characters at both ends of the string####";<🎜> <🎜>$str1 = trim($str,"#");<🎜> <🎜>//Pass in the second parameter to the function trim, <🎜> <🎜>trim will delete the # characters at both ends of the string $str echo $str."
"; echo $str1; ?>
Output: ##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 http://www.bkjia.com/PHPjc/985140.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/985140.htmlTechArticleA simple guide to using the trim() function in PHP string trim ( string $str [, string $charlist ] ) - remove Blank characters (or other characters) at the beginning and end of the string The trim() function takes the second parameter...
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