PHP trim () function simple user Guide, Trim usage Guide _php tutorial

Source: Internet
Author: User

A simple guide to using the trim () function in PHP, Trim usage Guide


String Trim (String $str [, String $charlist])-removes whitespace characters (or other characters) from the beginning and end of a string

Trim () function when the second argument is empty, the default is to remove spaces, tabs, newline characters, carriage returns, vertical tabs, and so on, when the second argument is added

Copy the Code code as follows:
1) Trim (' \ ' string\ ', ' \ ' sg '); Final output: \ "Strin
2) Trim (' \ ' string\ ', ' \ ' sg '); Final output: \ "String\"
2) Trim (' \ ' string\ ', ' \ ' sg '); Final output: Trin

So the trim () function takes precedence over whitespace characters from the first and the end of the character, and then filters out the given character to be removed (list), and also applies to the LTrim (), RTrim () function

Definition and usage

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

Grammar

Trim (str,charlist) parameter 1 str is the string to be manipulated, parameter 2 charlist optional, specifies the special symbol you want to remove.

If the second parameter does not give a value, the preset removes the following characters:

"(ASCII (0x20)), an ordinary space.
"\ T" (ASCII 9 (0x09)), a tab.
"\ n" (ASCII (0x0A)), a new line (line feed).
"\ R" (ASCII (0x0D)), a carriage return.
"\" (ASCII 0 (0x00)), the Nul-byte.
"\x0b" (ASCII One (0x0B)), a vertical tab.
If you want to remove other characters, you can set it in the second parameter.

Example 1

<?php $str = "Use function trim to remove whitespace characters at both ends of the string"; $str 1 = Trim ($STR); echo "Before processing". strlen ($STR). " Characters "; echo "
"; www.phpjc.cn echo "

Output:

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

Example 2

<?php $str = "# #使用函数trim去掉字符串两端特定字符 # # #; $str 1 = Trim ($str, "#"); When you pass in the second parameter for function trim, Trim removes the # character Echo $str at both ends of the string $str. "

Output:

# #使用PHP函数trim () remove specific characters at both ends of a string # # # # Use function trim to remove specific characters at both ends

The above mentioned is the whole content of this article, I hope you can like.

http://www.bkjia.com/PHPjc/985279.html www.bkjia.com true http://www.bkjia.com/PHPjc/985279.html techarticle a simple guide to using the trim () function in PHP, Trim usage Guide, string trim (string $str [, String $charlist])-Removes the white space character (or other characters) trim () function at the beginning and end of a string ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.