The trim()
function strips spaces, new lines, and tabs from either side of a string.
PHP trim() Function has the following syntax.
string trim ( string str [, string trim_chars] )
The optional second parameter to trim() sets the individual characters you want it to trim.
PHP trim() Function returns the trimmed string value.
There are two minor variants to trim()
.
ltrim()
and rtrim()
do the same thing,
but only trim from the left and right respectively.
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.