Showing posts with label PHP Trim Function Tutorial. Show all posts
Showing posts with label PHP Trim Function Tutorial. Show all posts

Monday, April 30, 2012

PHP Trim Function Tutorial


Trim Function
Syntax:
trim(string,charlist)

The trim() function takes two parameters.
The trim() function strips whitespaces or other characters from the both sides i.e. beginning and end of a string.
If the second argument, charlist is not specified, trim() function strips all whitespace (" "), new line "\n", tab ("\t"), carriage return ("\r") by default. 
Example: 1
        <?php
       $str = '&nbsp;This is a test.&nbsp;';
       $str_trimed = trim($str);
       echo $str_trimed;
       echo $str;
        ?>
Output
 This is a test.  This is a test. 

Example: 2
       <?php
       $str = '?My workload is increasing. I am learning to prioritize.?';
       $str_trimed = trim($str, "?");
       echo $str;
       echo "<br>";
       echo $str_trimed;
       ?>
Output
?My workload is increasing. I am learning to prioritize.?
My workload is increasing. I am learning to prioritize.

Notice that '?' is trimmed both sides from string.