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:
- " " (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.
- "" (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 "before processing". strlen ($ str). "characters"; echo "<br/> ";
- // Www.phpjc.cn echo "<br/> ";
- Echo "after processing with 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 = "## remove specific characters at both ends of the string using the trim function ####";
- $ Str1 = trim ($ str ,"#");
- // Input the second parameter for the trim function,
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