substr 


string apg

RETURN part of a STRING.





The $string can have only one character.

If $start = 0 the returned STRING starts at the beginning of $string.

If $start > 0 the returned STRING starts at the position referenced by $start in the $string.

If $start < 0 the returned STRING starts at the ending of $string.

If $length > 0 is given, the STRING returned will contain at most length characters beginning from $start, (depending on the length of $string).

If $length < 0 is given, then that many characters will be omitted from the end of STRING, (after the $start position has been calculated when a $start < 0).

If $start denotes the position of this truncation or beyond, FALSE will be returned.

If $length = 0 is given, FALSE or NULL, an empty STRING will be returned.

If $length is omitted, the substring starting from $start until the end of the STRING will be returned.



<?php

str substr 
( str $string , int $start [, int $length ] )


where,

$string = The input STRING

$start 
= The position to begin the search

$length 
= The available length

?>

$string


The input STRING.



$start


The position to begin the search.



$length


The available length.



  1 EXERCISE   

<?php

$str01 
= 'Life is not a bed of roses';

for(
$x01= 0; $x01 <= strlen($str01); $x01++)
{
    echo 
substr($str01, $x01) . '<br>';
}

?>

 RESULT   

Life is not a bed of roses
ife is not a bed of roses
fe is not a bed of roses
e is not a bed of roses
is not a bed of roses
is not a bed of roses
s not a bed of roses
not a bed of roses
not a bed of roses
ot a bed of roses
t a bed of roses
a bed of roses
a bed of roses
bed of roses
bed of roses
ed of roses
d of roses
of roses
of roses
f roses
roses
roses
oses
ses
es
s


  2 EXERCISE   

<?php

$str02 
= 'Life is not a bed of roses';

for(
$x02= strlen($str02); $x02 >= 0; $x02--)
{
    echo 
substr($str02, $x02) . '<br>';
}

?>

 RESULT   

s
es
ses
oses
roses
roses
f roses
of roses
of roses
d of roses
ed of roses
bed of roses
bed of roses
a bed of roses
a bed of roses
t a bed of roses
ot a bed of roses
not a bed of roses
not a bed of roses
s not a bed of roses
is not a bed of roses
is not a bed of roses
e is not a bed of roses
fe is not a bed of roses
ife is not a bed of roses
Life is not a bed of roses


  3 EXERCISE   

<?php

$str03 
= 'Life is not a bed of roses';

$x03 = mt_rand(0, strlen($str03));

// echo 'Substring "' . 
   
substr($str03, 0, $x03) . 
    
'" with length ' . $x03 . '<br>';

?>

  4 EXERCISE   

<?php

$str04 
= 'Life is not a bed of roses';

$x04 = mt_rand(0, strlen($str04));

echo 
'Substring "' . substr($str04, -$x04, 1) . 
                                 
'" with length ' . $x04 . '<br>';

?>

  5 EXERCISE   

<?php

$str05 
= 'Life is not a bed of roses';

$x05a = -3;

$x05b = -1;

if(
substr($str05, $x05a, $x05b) == TRUE)
{
echo 
'Substring "' . substr($str05, $x05a, $x05b) . 
'" with length ' . abs($x05a - $x05b)  . 
' from [' . $x05a . ' to ' . $x05b . ']<br><br>';
}
else
{
var_dump(substr($str05, $x05a, $x05b));
}

if(
substr($str05, $x05b, $x05a) == TRUE)
{
    
echo 
'Substring "' . substr($str05, $x05b, $x05a) . 
'" with length ' . abs($x05b - $x05a)  . ' from [' . 
$x05b . ' to ' . $x05a . ']<br><br>';
}
else
{
var_dump(substr($str05, $x05b, $x05a));
}    

?>

  6 EXERCISE   

<?php

$strings_array 
= [ NULL, "", 12345, 
                                      
"abcdef", 
                                      
"123abc", 
                                      
"_123abc" ];

$counter = 1;
foreach (
$strings_array as $str) {
  
/* variations with two arguments */
  /* start values >, < and = 0    */

  
echo ("<br><br>Iteration: ".$counter."<br>");
  echo (
"Variations for two arguments:<br>");
  
var_dump ( substr($str, 1) );
  echo 
'<br>';
  
var_dump ( substr($str, 0) );
  echo 
'<br>';
  
var_dump ( substr($str, -2) );

  
/* variations with three arguments */
  /* start value variations with length values  */

  
echo ("<br><br><br>Variations for three arguments:<br>");
  
var_dump ( substr($str, 1, 3) );
  echo 
'<br>';
  
var_dump ( substr($str, 1, 0) );
  echo 
'<br>';
  
var_dump ( substr($str, 1, -3) );
  echo 
'<br>';
  
var_dump ( substr($str, 0, 3) );
  echo 
'<br>';
  
var_dump ( substr($str, 0, 0) );
  echo 
'<br>';
  
var_dump ( substr($str, 0, -3) );
  echo 
'<br>';
  
var_dump ( substr($str, -2, 3) );
  echo 
'<br>';
  
var_dump ( substr($str, -2, 0 ) );
  echo 
'<br>';
  
var_dump ( substr($str, -2, -3) );

  
$counter++;
}

/* variation of start and length 
                  to point to same element */
echo ("<br><br><br>Testing for variations of 
             start and length to point to same element:<br>"
);
var_dump (substr("abcde" , 2, -2) );
echo 
'<br>';
var_dump (substr("abcde" , -3, -2) );

/* Testing to return empty string 
 * when start denotes the position beyond 
 * the truncation (set by negative length) */
 
echo ("<br><br><br>Testing for start > truncation:<br>");
var_dump (substr("abcdef" , 4, -4) );

/* String with null character */
echo ("<br><br><br>Testing for string with null characters:<br>");
var_dump (substr("abc\x0xy\x0z" ,2) );

/* String with international characters */
echo ("<br><br><br>Testing for string with international characters:<br>");
var_dump (substr('\xIñtërnâtiônàlizætiøn',3) );

/* start <0 && -start > length */
echo "<br><br><br>Start before the first char:<br>";
var_dump (substr("abcd" , -8) );

/* Omitting length and passing a NULL length */
echo "<br><br><br>Omitting length or using NULL length:<br>";
var_dump (substr("abcdef" , 2) );
echo 
'<br>';
var_dump (substr("abcdef" , 2, NULL) );

?>