PHP Trim function

Trim White Space in PHP

In PHP when you want to remove the blank spaces which are also called white spaces then you then you can use ltrim ( ) and rtrim ( ) function in. Below we take some example to explain these two function.
Suppose you have a form and in one text box you ask for enter the name. Some one enter the name but give some spaces before and after his/her name.
For example " Kiren Noreen ":
Above name contain two spaces before and two spaces after the name. When you store this name in your database with these spaces then this is very bad practices for programmer when some one want to search "Kiren Noreen" . its return zero record, because your name is store with spaces, So it is very necessary that you must remove these spaces before and after the string.

ltrim( ) Function

This function remove the spaces before the string.


<?php
$name= "     Kiren Noreen";
$name=ltrim($name);
echo $name;
?>

This will return
"Kiren Noreen"

rtrim( ) Function

This function remove the spaces after the string.


<?php
$name= "Kiren Noreen       ";
$name=rtrim($name);
echo $name;
?>

This will return
"Kiren Noreen"

trim( ) Function

This function remove the All spaces before, after and between the string . For example if you have a name " Kiren Noreen ". This name contain two speces before the name and two after the name and one between the name. if you apply the trim function then it will give the output without spaces.

 
<?php
$name= "  Kiren Noreen  ";
$name=trim($name);
echo $name;
?>

This will return
"KirenNoreen"