Simple guide to using the trim () function in PHP

Source: Internet
Author: User
Tags strlen trim

String Trim (String $str [, String $charlist])-To remove white space characters (or other characters) at the end of a string

The trim () function removes spaces, tabs, line breaks, carriage returns, vertical tabs, and so on, by default when the second argument is empty, when the second argument 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

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

Definitions and usage

The PHP function trim () deletes white-space 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, specifying the special symbol you want to remove.

If the second argument is not given a value, the preset removes the following characters:

"(ASCII (0x20)), an ordinary space.
"T" (ASCII 9 (0x09)), 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 argument.

Example 1

1 2 3 4 5 6 7 <?php $str = "Use function trim to remove whitespace characters from both ends of the string"; $str 1 = Trim ($STR); echo "Before processing has". strlen ($STR). Characters "; echo "<br/>"; www.phpjc.cn echo "<br/>"; echo "processed with the Trim function". strlen ($str 1). Characters ";?>

Output:

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

Example 2

1 2 3 4 5 6 7 <?php $str = "# #使用函数trim去掉字符串两端特定字符"; $str 1 = Trim ($str, "#"); Pass the second argument to the function trim, and trim deletes the # character Echo $str at both ends of the string $str. <br> "; echo $str 1;?>

Output:

# #使用PHP函数trim () remove specific characters at both ends of the string use function trim to remove specific characters at both ends of the 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.