How to remove white spaces from beginning and/or the end of a string in PHP
By Siddu S Hosageri
Remove white spaces from a string using php?
Teacher SiliconIndia replied to Siddu S Hosageri Tuesday, October 27, 2009
Hi siddu,
White spaces can be removed from many php functions as below.
trim() - Remove white space characters from the beginning and the end of a string.
ltrim() - Remove white space characters from the beginning of a string.
rtrim() - Remove white space characters from the end of a string.
chop() - Same as rtrim().
Here is a PHP script example of trimming strings:
<?php
$text = "t t Hello world!t t ";
$leftTrimmed = ltrim($text);
$rightTrimmed = rtrim($text);
$bothTrimmed = trim($text);
print("leftTrimmed = ($leftTrimmed)n");
print("rightTrimmed = ($rightTrimmed)n");
print("bothTrimmed = ($bothTrimmed)n");
?>
This script will print:
leftTrimmed = (Hello world! )
rightTrimmed = ( Hello world!)
bothTrimmed = (Hello world!)