|  | 
 
 
  substr_replaceReplace text within a portion of a string 
  Description
   stringarray substr_replace(arraystring
 $string,arraystring
 $replace,arrayint
 $offset,arrayintnull
 $length=null)
 
  Parameters
    
    
     
string
      
       The input string.
       
       An array of strings can be provided, in which
       case the replacements will occur on each string in turn. In this case,
       the replace,offsetandlengthparameters 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.
replace
      
       The replacement string.
      
offset
      
       If offsetis non-negative, the replacing will
       begin at theoffset'th offset intostring. 
       If offsetis negative, the replacing will
       begin at theoffset'th character from the
       end ofstring.
length
      
       If given and is positive, it represents the length of the portion of
       stringwhich is to be replaced. If it is
       negative, it represents the number of characters from the end ofstringat which to stop replacing. If it
       is not given, then it will default to strlen(string); i.e. end the replacing at the
       end ofstring. Of course, iflengthis zero then this function will have the
       effect of insertingreplaceintostringat the givenoffsetoffset. 
  Return Values
   The result string is returned. If stringis an
   array then array is returned. 
  Examples
    
    Example #1 Simple substr_replace examples 
<?php
$var = 'ABCDEFGH:/MNRPQR/';
echo "Original: $var<hr />\n";
/* These two examples replace all of $var with 'bob'. */
echo substr_replace($var, 'bob', 0) . "<br />\n";
echo substr_replace($var, 'bob', 0, strlen($var)) . "<br />\n";
/* Insert 'bob' right at the beginning of $var. */
echo substr_replace($var, 'bob', 0, 0) . "<br />\n";
/* These next two replace 'MNRPQR' in $var with 'bob'. */
echo substr_replace($var, 'bob', 10, -1) . "<br />\n";
echo substr_replace($var, 'bob', -7, -1) . "<br />\n";
/* Delete 'MNRPQR' from $var. */
echo substr_replace($var, '', 10, -1) . "<br />\n";
?>
 
    
    Example #2 
     Using substr_replace to replace multiple strings at
     once
     
<?php
$input = array('A: XXX', 'B: XXX', 'C: XXX');
// A simple case: replace XXX in each string with YYY.
echo implode('; ', substr_replace($input, 'YYY', 3, 3))."\n";
// A more complicated case where each replacement is different.
$replace = array('AAA', 'BBB', 'CCC');
echo implode('; ', substr_replace($input, $replace, 3, 3))."\n";
// Replace a different number of characters each time.
$length = array(1, 2, 3);
echo implode('; ', substr_replace($input, $replace, 3, $length))."\n";
?>
 The above example will output:
A: YYY; B: YYY; C: YYY
A: AAA; B: BBB; C: CCC
A: AAAXX; B: BBBX; C: CCC
 
  NotesNote: This function is
binary-safe.
 |