Introduction to the definition and syntax of PHP function trim ()

Source: Internet
Author: User

You are writingDefinition 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:

 
 
  1. " " (ASCII 32 (0x20)), an ordinary space.   
  2. "t" (ASCII 9 (0x09)), a tab.   
  3. "n" (ASCII 10 (0x0A)), a new line (line feed).   
  4. "r" (ASCII 13 (0x0D)), a carriage return.   
  5. "" (ASCII 0 (0x00)), the NUL-byte.   
  6. "x0B" (ASCII 11 (0x0B)), a vertical tab.  

If you want to remove other characters, you can set them in the second parameter.

Example 1

 
 
  1. <? Php
  2. $ Str = "Remove blank characters at both ends of the string using the trim function ";
  3. $ Str1 = trim ($ str );
  4. Echo "before processing". strlen ($ str). "characters"; echo "<br/> ";
  5. // Www.phpjc.cn echo "<br/> ";
  6. Echo "after processing with the trim function". strlen ($ str1). "characters ";
  7. ?>

Output:

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

Example 2

 
 
  1. <? Php
  2. $ Str = "## remove specific characters at both ends of the string using the trim function ####";
  3. $ Str1 = trim ($ str ,"#");
  4. // Input the second parameter for the trim function,
    Trim will delete the # character echo $ str at both ends of the string $ str. "<br> ";
  5. Echo $ str1;
  6. ?>

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
 


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.