Rechercher une fonction PHP

substr_replace

(PHP 4, PHP 5, PHP 7)

substr_replaceReplace text within a portion of a string

Description

substr_replace ( mixed $string , mixed $replacement , mixed $start [, mixed $length ] ) : mixed

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

PHP: substr_replace - Manual Home of Manuel PHP  Contents Haut

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 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.

replacement

The replacement string.

start

If start is non-negative, the replacing will begin at the start'th offset into string.

If start is negative, the replacing will begin at the start'th character from the end of string.

length

If given and is positive, it represents the length of the portion of string which is to be replaced. If it is negative, 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 is zero then this function will have the effect of inserting replacement into string at the given start offset.

PHP: substr_replace - Manual Home of Manuel PHP  Contents Haut

Return Values

The result string is returned. If string is an array then array is returned.

PHP: substr_replace - Manual Home of Manuel PHP  Contents Haut

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'0strlen($var)) . "<br />\n";

/* Insert 'bob' right at the beginning of $var. */
echo substr_replace($var'bob'00) . "<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'33))."\n";

// A more complicated case where each replacement is different.
$replace = array('AAA''BBB''CCC');
echo 
implode('; 'substr_replace($input$replace33))."\n";

// Replace a different number of characters each time.
$length = array(123);
echo 
implode('; 'substr_replace($input$replace3$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

PHP: substr_replace - Manual Home of Manuel PHP  Contents Haut

Notes

Note: This function is binary-safe.

PHP: substr_replace - Manual Home of Manuel PHP  Contents Haut

See Also

Find a PHP function

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 30/01/2003, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/php-rf-substr-replace.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.

References

  1. View the html document Language of the document:fr Manuel PHP : http://php.net

These references and links indicate documents consulted during the writing of this page, or which may provide additional information, but the authors of these sources can not be held responsible for the content of this page.
The author This site is solely responsible for the way in which the various concepts, and the freedoms that are taken with the reference works, are presented here. Remember that you must cross multiple source information to reduce the risk of errors.

Contents Haut