Home  >  Article  >  Backend Development  >  Definition and syntax introduction of PHP function trim()_PHP tutorial

Definition and syntax introduction of PHP function trim()_PHP tutorial

WBOY
WBOYOriginal
2016-07-15 13:30:15915browse

Everyone is writingDefinition and usage

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

Syntax

trim(str,charlist) Parameter 1 str is the string to be operated on, 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:

<ol class="dp-xml">
<li class="alt"><span><span>" " (ASCII 32 (0x20)), an ordinary space.   </span></span></li>
<li><span>"t" (ASCII 9 (0x09)), a tab.   </span></li>
<li class="alt"><span>"n" (ASCII 10 (0x0A)), a new line (line feed).   </span></li>
<li><span>"r" (ASCII 13 (0x0D)), a carriage return.   </span></li>
<li class="alt"><span>"" (ASCII 0 (0x00)), the NUL-byte.   </span></li>
<li><span>"x0B" (ASCII 11 (0x0B)), a vertical tab.  </span></li>
</ol>

If you want to remove other characters, you can set in parameters.

Example 1

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><?</span><span class="tag-name">php</span><span> </span></span></li><li class="alt"><span><span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">" 使用函数trim去掉字符串两端空白字符 "</span><span>; </span></span></li><li class="alt"><span><span>$</span><span class="attribute">str1</span><span> = </span><span class="attribute-value">trim</span><span>($str); </span></span></li><li class="alt"><span><span>echo "处理前有".strlen($str)."个字符"; echo "</span><span class="tag"><</span><span class="tag-name">br</span><span class="tag">/></span><span>"; </span></span></li>
<li class="alt"><span><span>//www.phpjc.cn echo "</span><span class="tag"><</span><span class="tag-name">br</span><span class="tag">/></span><span>"; </span></span></li>
<li class="alt"><span><span>echo "使用trim函数处理后有".strlen($str1)."个字符"; </span></span></li>
<li class="alt"><span><span class="tag">?></span><span> </span></span></li>
</ol>

Output:

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

Example 2

<ol class="dp-xml">
<li class="alt"><span><span class="tag"><?</span><span class="tag-name">php</span><span> </span></span></li><li class="alt"><span><span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">"##使用函数trim去掉字符串两端特定字符####"</span><span>; </span></span></li><li class="alt"><span><span>$</span><span class="attribute">str1</span><span> = </span><span class="attribute-value">trim</span><span>($str,"#"); </span></span></li><li class="alt"><span><span>//为函数trim传入第二个参数,<br />trim将删除字符串$str两端的#字符 echo $str."</span><span class="tag"><</span><span class="tag-name">br</span><span class="tag">></span><span>"; </span></span></li>
<li class="alt"><span><span>echo $str1; </span></span></li>
<li class="alt"><span><span class="tag">?></span><span> </span></span></li>
</ol>

Output:

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


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446304.htmlTechArticleEveryone is writing the definition and usage of PHP function trim() to remove whitespace characters and other predefined characters from both ends of a string character. Syntax trim(str,charlist) Parameters 1 str is the string to be operated on, parameters...
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