substr_replace 


string apg

COMPARE two strings from an offset, up to length characters.





This function replaces a copy of $string delimited by the $start and , (optionally), $length parameters with the string given in $replacement.

An ARRAY of STRINGS can be provided, in which case the replacements will occur on each string in turn.

In this case, the $replacement, $start and $length parameters may be provided either as scalar values to be applied to each input STRING in turn, or as ARRAYS, in which case the corresponding array element will be used for each input STRING.

If $start < 0 the replacing will begin at the $start'th character from the end of $string.

If $start > 0 the replacing will begin at the $start'th character offset into the $string.

If $start = 0 the replacing will begin at the 0 character into the $string.

If $length is given and is $length > 0, it represents the length of the portion of $string which is to be replaced.

If it is $length < 0, it represents the number of characters from the end of $string at which to stop replacing.

If it is not given, then it will default to strlen($string); i.e. end the replacing at the end of $string.

Of course, if $length = 0 then this function will have the effect of inserting $replacement into $string at the given $start offset.


This function is binary-safe

In PHP, some functions are marked as binary-safe, it means that the functions works correctly even when you pass binary data.



<?php

mix substr_replace 
mix $string 
                                
mix $replacement 
                                
mix $start [, 
                                
mix $length ] ) 


where,

$string The input STRING or ARRAY of STRINGS

$replacement 
The replacement STRING

$start 
The position where the replacement begins

$length 
The length of the portion os 
                                the STRING provided will be replaced

?>

$string


The replacement STRING or ARRAY of STRINGS.



$replacement


The replacement STRING..



$start


The $start where to begin the replacement.



$length


The porcion os the $string which is to replaced.



  1 EXERCISE   

<?php 

$str01 
"Everything what is said on something, 
does not have to be necessarily a REPLY!"
;

$strsb01a "TRUE!"
$strsb01b "LIE!"

echo 
'$str01 = ' $str01 '<br>'
echo 
'$strsb01a = ' $strsb01a '<br>'
echo 
'$strsb01b = ' $strsb01b '<br><br>'

echo 
'<br><br>$nwstrsb01a = substr_replace ($str01, 
                                                  $strsb01a, 72)<br><br>'

$nwstrsb01a substr_replace ($str01$strsb01a72); 
echo 
'$nwstrsb01a = ' .  $nwstrsb01a '<br><br><br>'

echo 
'$nwstrsb01b = substr_replace ($str01, $strsb01b, -6)<br><br>'
$nwstrsb01b substr_replace ($str01$strsb01b, -6); 

echo 
'$nwstrsb01b = ' $nwstrsb01b

?> 

 RESULT   

Everything what is said on something, does not have to be necessarily a REPLY!

TRUE!
LIE!

substr_replace ($str01, $strsb01a, 72)
Everything what is said on something, does not have to be necessarily a TRUE!

substr_replace ($str01, $strsb01b, -6)
Everything what is said on something, does not have to be necessarily a LIE!


  2 EXERCISE   

<?php

$arr02 
= ['REPLY''bool'];

var_dump($arr02);

echo 
'<br><br><br>';

$nwarr02a substr_replace (["REPLY""bool"], "TRUE"0);
var_dump($nwarr02a);

echo 
'<br><br><br>';

$nwarr02b substr_replace (["REPLY""bool"], "FALSE"0);

var_dump($nwarr02b);

?> 

 RESULT   

array(2) { [0]=> string(5) "REPLY" [1]=> string(4) "bool" }

array(2) { [0]=> string(4) "TRUE" [1]=> string(4) "TRUE" }

array(2) { [0]=> string(5) "FALSE" [1]=> string(5) "FALSE" }



  3 EXERCISE   

<?php

$arr03 
= ['REPLY''RESPOSTA''bool'];

echo 
'<pre>';

var_dump($arr03);

echo 
'<br><br>';

$nwarr03a substr_replace ($arr03" TRUE"8);
var_dump($nwarr03a);

echo 
'<br><br>';

$nwarr03b substr_replace ($arr03" FALSE"8);

var_dump($nwarr03b);

echo 
'</pre>';

?> 

  4 EXERCISE   

<?php

$arr04 
= ['REPLY''bool'];

var_dump($arr04);

echo 
'<br><br>';

$arrr04f = [' FALSE'];

var_dump($arrr04f);

echo 
'<br><br>';

$arrr04t = [' TRUE'];

var_dump($arrr04t);

echo 
'<br><br><br>';

$nwarr04a substr_replace ($arr04$arrr04f81);
var_dump($nwarr04a);

echo 
'<br><br>';

$nwarr04b substr_replace ($arr04$arrr04t10, -1);

var_dump($nwarr04b);

?> 

  5 EXERCISE   

<?php

$arr 
= array('abc' => 'llsskdkk',
                     
'def' => 'llsskjkkdd'
                         
=> 'hello'
                       
42 => 'world');
                     
$newarr substr_replace($arr'zzz'0, -2);

echo 
'<pre>';
print_r($newarr);
echo 
'</pre>';

?>

  6 EXERCISE   

<?php

/*
 * Testing substr_replace() for error conditions
*/

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

$s1 "Good morning";

echo 
"<br>Testing substr_replace() function 
            with start and length as arrays but string not:<br>"
;
try {
    
var_dump(substr_replace($s1"evening", array(5)));
} catch (
TypeError $e) {
    echo 
$e->getMessage(), "<br><br>";;
}
try {
    
var_dump(substr_replace($s1"evening"5, array(1)));
} catch (
TypeError $e) {
    echo 
$e->getMessage(), "<br>";;
}

?>