Forum : How to remove white spaces from beginning and/or the end of a string in PHP
Brief description  about Online courses   join in Online courses
View Siddu S Hosageri 's Profile

How to remove white spaces from beginning and/or the end of a string in PHP

Remove white spaces from a string using php?
Asked by Siddu S Hosageri | Oct 27, 2009 |  Reply now
Replies (1)
View teacher siliconindia 's Profile
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!)
Oct 27, 2009