Trimming

Remove whitespace, or other specified character, from a string

 

Trim from both sides of a string

trim(string [,charlist]) ; //optional charlist

<?php
	$myString = "      A string with whitespace either side        " ;

	echo "Start:" . trim($myString) . ":End" ;
?>

 

Trim from left side of a string

ltrim(string [,charlist]) ; //optional charlist

<?php
	$myString = "      A string with whitespace either side        " ;

	echo "Start:" . ltrim($myString) . ":End" ;
?>

 

Trim from right side of a string

rtrim(string [,charlist]) ; //optional charlist

<?php
	$myString = "      A string with whitespace either side        " ;

	echo "Start:" . rtrim($myString) . ":End" ;
?>

 

If charlist is not used, the following will be removed:

" " Empty space
"\t" Tab
"\n" New Line
"r" Carriage Return
"\0" NULL
"\x0B" Vertical Tab

Leave a Reply