PHP String trim()函数
PHP字符串修剪()函数是内置函数。它用于删除字符串开头和结尾的空格,或删除字符串两侧的空格。
语法
trim(string,charlist);
Parameter | Description | Required/Optional |
---|---|---|
String | 指定要检查的字符串。 | 必需 |
charlist | 指定要移除的字符。 以下字符: | 可选 |
- “
\0
” – 空字符 - “
\t
” – 制表符 - “
\n
” – 换行符 - “
\x0B
” – 垂直制表符 - “
\r
” – 回车符 - “
” – 普通空格
示例1
<?php
str = "Hello PHP!";
echo "Your string is: ".str ."<br>";
echo "By using trim() function :".trim($str,"Hed!");
?>
输出:
Your string is: Hello PHP!
By using trim() function :llo PHP
示例2
<?php
str = " Hello Javatpoint! ";
echo "Without trim() function: ".str;
echo "<br>";
echo "With trim() function: ".trim($str);
?>
输出:
Without trim() function: Hello Javatpoint!
With trim() function: Hello Javatpoint!
示例3
<?php
str = "\n\n\nHello Javatpoint\n\n\n";
echo "Without trim() function: " .str;
echo "<br>";
echo "With trim() function:" . trim($str);
?>
输出:
Without trim() function: Hello Javatpoint
With trim() function:Hello Javatpoint