PHP trim() Function

Definition

The trim() function strips spaces, new lines, and tabs from either side of a string.

Syntax

PHP trim() Function has the following syntax.

string trim ( string str [, string trim_chars] )

Parameter

The optional second parameter to trim() sets the individual characters you want it to trim.

Return

PHP trim() Function returns the trimmed string value.

Note

There are two minor variants to trim(). ltrim() and rtrim() do the same thing, but only trim from the left and right respectively.

Example

Trim a string


<?PHP//  ww w  . j  av  a2 s. co m
$a = trim(" testing from java2s.com "); 
print $a;
print "\n";
$b = trim(" testing from java2s.com", "com"); 
print $b;
?>

The code above generates the following result.





















Home »
  PHP Tutorial »
    Function reference »




PHP Array Functions
PHP Calendar Functions
PHP Class Functions
PHP Data Type Functions
PHP Date Functions
PHP File Functions
PHP Image Functions
PHP Math Functions
PHP MySQLi Functions
PHP SimpleXML Functions
PHP String Functions
PHP XML Functions
PHP Zip Functions