PHP trim

trim() – Trimming White Spaces in variable.

The trim() function removes white spaces and other predefined characters from both sides of a string.

For example consider this,

<?php

$space = " username ";

$strcount = strlen($space);
print $strcount;

?>

It prints 10. Assume that you comparing this username with your table records for login authorization. In this case it doesn’t return anything even if you have one record with this name in table. You may use this trim() function for trimming white spaces. See below example,

<?php

$space = trim(" username ");

$strcount = strlen($space);
print $strcount;

?>

Now it prints 8.

Two related function are ltrim( ) and rtrim( ). The first one, ltrim( ), removes space from the beginning of a string; the second one, rtrim( ), removes space from the end of a string.

Posted in Php