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

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

Example 2

Output

Example 3

Output

Example 4

Output