PHP Trim()- String functions

Trim() is a php function that strips whitespaces from the beginning and end of a string.
For example:
$mystring = trim( " The free php sources ") ;
echo $mystring ;
Display:
The free php sources //No whitespaces

What characters do Trim() strip ?
Trim function will strip the following characters:
  • " " (ASCII 32 ): an ordinary space.
  • "\t" (ASCII 9 ): a tab.
  • "\n" (ASCII 10 ): a new line (line feed).
  • "\r" (ASCII 13 ): a carriage return.
  • "\0" (ASCII 0 ): the NUL-byte.
  • "\x0B" (ASCII 11 ): a vertical tab.
Can I trim other characters:
Yes, this is possible and very easy, you just use a second parameter with all characters you want to stim.

Example: Trim these characters from the beginning and end of a string: "s" , "T" , "h".

$mystring = trim( "The free php sources" , "sTh") ;
echo $mystring ;
Display:

e free php source
Trim() is case-sensitive, that's why we used "sTh" and not "sth".

Trim from only the beginning or the end of a string:
To trim from the beginning, use ltrim() , and to trim from the end use rtrim()

0 comments: