substr_replace

(PHP 4, PHP 5, PHP 7, PHP 8)

substr_replaceReplace text within a portion of a string

Description

substr_replace(
    array|string $string,
    array|string $replace,
    array|int $offset,
    array|int|null $length = null
): string|array

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

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

replace

The replacement string.

offset

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

If offset is negative, the replacing will begin at the offset'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 replace into string at the given offset offset.

Return Values

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

Changelog

Version Description
8.0.0 length is nullable now.

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

Notes

Note: This function is binary-safe.

See Also

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial