ltrim 


string apg

STRIP whitespaces and selected characters from the beginning of a STRING.





This function returns a string with whitespace stripped from the beginning of $str if $character_mask is not provided.



<?php

str ltrim 
( str $str [, str $character_mask ] )


where,

$str = The input STRING

$character_mask 
= The list of additional characters 
                                          to strip from the beginning

?>
 

$str


The input STRING.



$character_mask

The additional characters list to be striped:

DEFAULT ASCII VALUE
" " 32 0x20 ESPAÇO
"\t" 9 0x09 TAB H
"\n" 10 0x0A NL
"\r" 13 0x0D CR
"\0" 0 0x00 NULL
"\x0B" 11 0x0B TAB V
ed48


  1 EXERCISE   

<?php

$strl01a 
= '            QUOSQUE TANDEM?          ';

$strl01b = ltrim($strl01a);

echo 
$strl01b . '<br><br>';


$strl01c = '            QUOSQUE TANDEM?          ';

$strl01d = ltrim($strl01c);
    
echo 
$strl01d;

?> 

 RESULT   

QUOSQUE TANDEM?

QUOSQUE TANDEM?


  2 EXERCISE   

 <?php

$strl02a 
= '-/--//---///----QUOSQUE TANDEM?-/--//---///----';

$strl02b = ltrim($strl02a, '"-", "/"');

echo 
$strl02b . '<br><br>';

/* ---------------------------------------

    list of characters defined by
   a range of ASCII hexadecimal values
   
   '0x29,..0x30' -> from 0x29 to 0x30
   
   --------------------------------------- */

$strl02c = '-/--//---///----QUOSQUE TANDEM?-/--//---///----';

$strl02d = ltrim($strl02c, '0x29, ..0x30');
    
echo 
$strl02d;

?> 

 RESULT   

QUOSQUE TANDEM?-/--//---///----

QUOSQUE TANDEM?-/--//---///----


  3 EXERCISE   

<?php

echo "Testing ltrim() : basic functionality.<br>";

$text  = " \t\r\n\0\x0B ---These are a few words---  ";
$hello  = "!===Hello World===!";
$binary = "\x09\x0AExample string";
$alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

echo 
"<br><br>Trim string with all white space characters:<br>";
var_dump(ltrim($text));

echo 
"<br><br>Trim non-whitespace from a string:<br>";
var_dump(ltrim($hello, "=!"));

echo 
"<br><br>Trim some non-white space 
                   characters from a string:<br>"
;
var_dump(ltrim($hello, "!oleH="));

echo 
"<br><br>Trim some non-white space 
            characters from a string suing a character range:<br>"
;
var_dump(ltrim($alpha, "A..Z"));

echo 
"<br><br>Trim the ASCII control 
                       characters at the beginning of a string:<br>"
;
var_dump(ltrim($binary, "\x00..\x1F"));

?>

  4 EXERCISE   

<?php

/*  Testing for Error conditions  */

/*  Invalid Number of Arguments */

 
echo "<br><br>Output Conditions:<br>";

/* heredoc string */
$str = <<<EOD
us
ing heredoc string
EOD;

echo 
"<br><br>Using heredoc string:<br>";
var_dump( ltrim($str, "\nusi") );

/* Testing the Normal behaviour of ltrim() function */

 
echo "<br><br>Output for Normal Behaviour:<br>";
 
var_dump ( ltrim(" \t\0    ltrim test") );                      
 
/* without second Argument */
 
echo '<br><br>';
 
var_dump ( ltrim("   ltrim test" , "") );                       
 
/* no characters in second Argument */
  
echo '<br><br>';
 
var_dump ( ltrim("        ltrim test", NULL) );                 
 
/* with NULL as second Argument */
  
echo '<br><br>';
 
var_dump ( ltrim("        ltrim test", true) );                 
 
/* with boolean value as second Argument */
  
echo '<br><br>';
 
var_dump ( ltrim("        ltrim test", " ") );                  
 
/* with single space as second Argument */
  
echo '<br><br>';
 
var_dump ( ltrim("\t\n\r\0\x0B ltrim test", "\t\n\r\0\x0B") );  
 
/* with multiple escape sequences as second Argument */
  
echo '<br><br>';
 
var_dump ( ltrim("ABCXYZltrim test", "A..Z") );                 
 
/* with characters range as second Argument */
  
echo '<br><br>';
 
var_dump ( ltrim("0123456789ltrim test", "0..9") );             
 
/* with numbers range as second Argument */
  
echo '<br><br>';
 
var_dump ( ltrim("@$#ltrim test", "#@$") );                     
 
/* with some special characters as second Argument */

 
echo "<br><br>Output for  scalar argument):<br>";
 
var_dump( ltrim(  12345  ) );                                   
 
/* Scalar argument */

 
echo "<br><br>Output for  NULL argument):<br>";
 
var_dump( ltrim(NULL) );                                        
 
/* NULL Argument */

?>

  5 EXERCISE   

<?php

echo "Testing ltrim() : error conditions.<br>";

$hello = "  Hello World\n";
echo 
"<br><br>Test ltrim function with 
                             various invalid charlists:<br>"
;
var_dump(ltrim($hello, "..a"));
 echo 
'<br><br>';
var_dump(ltrim($hello, "a.."));
 echo 
'<br><br>';
var_dump(ltrim($hello, "z..a"));
 echo 
'<br><br>';
var_dump(ltrim($hello, "a..b..c"));

?>