PHP trim() Function

PHP trim() Function

The trim() Function in PHP strips the whitespace or other characters from the beginning and the end of the given string.

The trim() function strips the following characters:

  • " " - an ordinary space.
  • "\t"- a tab.
  • "\n"- a new line (line feed).
  • "\r"- a carriage return.
  • "\0" -the NUL-byte.
  • "\x0B" -a vertical tab.

The related functions are as follows:

  1. ltrim()
  2. rtrim()

Syntax

trim ( string $str [, string $character_mask  ] )

Parameter

str(required)- This parameter specifies the string to be trimmed.

character_mask (optional)- This parameter specifies which characters to remove from the string.

Return

This function returns a string with whitespace stripped from the beginning and end of the specified string str.

Example 1

 

Output

Actual String: rePHP is an easy language to learn.
After trim() function...
New string: PHP is an easy language to learn. 

Example 2

 

Output

Actual String: 
PHP is an easy language to learn.
After trim() function...
New string: PHP is an easy language to learn. 

Example 3

 

Output

Actual String:                           PHP is an easy language to learn.                               

After trim() function...
New string: PHP is an easy language to learn. 

Example 4

 

Output

Actual String:                           PHP is an easy language to learn.                               
After trim() function...
New string:                              PHP is an easy language to learn.