Simple Usage Guide for trim () functions in PHP, and trim User Guide

Source: Internet
Author: User

Simple Usage Guide for trim () functions in PHP, and trim User Guide

String trim (string $ str [, string $ charlist])-removes spaces (or other characters) at the beginning and end of the string)

When the second parameter of the trim () function is null, spaces, tabs, line breaks, carriage returns, and vertical tabs are removed by default. When the second parameter is added

Copy codeThe Code is as follows:
1) trim ('\ "string \"', '\ "sg'); // final output: \" strin
2) trim ('\ "string \"', '\ "sg'); // final output: \" string \"
2) trim ('\ "string \"', '\ "sg'); // The final output is trin.

Therefore, the trim () function preferentially removes the spaces at the beginning and end of a character, and then filters out the given characters (lists) to be removed. This function is also applicable to the ltrim () and rtrim () functions.

Definition and usage

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

Syntax

Trim (str, charlist) parameter 1 str is the string to be operated, parameter 2 charlist (optional) specifies the special symbol to be removed.

If the second parameter is not set to a value, the following characters are 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.
"\ 0" (ASCII 0 (0x00), the NUL-byte.
"\ X0B" (ASCII 11 (0x0B), a vertical tab.
If you want to remove other characters, you can set them in the second parameter.

Example 1

<? Php $ str = "Remove blank characters at both ends of the string using the trim function"; $ str1 = trim ($ str); echo "available before processing ". strlen ($ str ). "characters"; echo "<br/>"; // www.phpjc.cn echo "<br/>"; echo "after processing using the trim function ". strlen ($ str1 ). "characters";?>

Output:

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

Example 2

<? Php $ str = "## use the trim function to remove specific characters at both ends of the string ###"; $ str1 = trim ($ str ,"#"); // input the second parameter for the function trim. trim will delete the # character echo $ str at both ends of the string $ str. "<br>"; 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

The above is all the content of this article. I hope you will like it.

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.