PHP Tutorial - 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
$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.