PHP String substr_replace() Function



The PHP String substr_replace() function is used to replace a portion of a string with another string. The position in the original string from which the replacement is to be done has to be given as a parameter.

If needed, the length of the replacement can be defined. An array of strings can be passed as an argument to this function, in which case the replacements will be performed on each string separately.

Syntax

Below is the syntax of the PHP String substr_replace() function −

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

Parameters

Here are the parameters of the substr_replace() function −

  • $string − (Required) It is the input string. If you give an array of strings, the replacements will be done to each one in turn.

  • $replace − (Required) It is the replacement string.

  • $offset − (Required) It is the place where to start replacing in the string. If it is not negative, the replacement will begin at the offset'th position in the string. If it is negative, the replacement will start at the offset'th character from the end of the string.

  • $length − (Optional) It determines how many characters should be replaced. The default length is the same as the string.

    A positive number shows the length of the string to be replaced. A negative value shows how many characters should be left at the end of the string after replacement. And 0 menas insert rather than replace.

Return Value

The substr_replace() function returns the replaced string. If the string is an array in that case the array will be returned.

PHP Version

First introduced in core PHP 4, the substr_replace() function continues to function easily in PHP 5, PHP 7, and PHP 8.

Example 1

Here is the basic example of the PHP String substr_replace() function to replace the string with another string.

<?php
   echo substr_replace("Tutorilas piont","Tutorials Point",0);
?>

Output

Here is the outcome of the following code −

Tutorials Point

Example 2

In the below PHP code we will use the substr_replace() function and replace a portion of a single string.

<?php
   // Original string
   $string = "Hello World!";

   // Replace "World" with "PHP"
   $result = substr_replace($string, "PHP", 6, 5);

   echo $result; 
?> 

Output

This will generate the below output −

Hello PHP!

Example 3

Now the below code shows how to insert string into a string without removing any characters using the substr_replace() function.

<?php
   // Original string
   $string = "Hello World!";

   // Insert string at position 6
   $result = substr_replace($string, "Beautiful ", 6, 0);

   echo $result; 
?> 

Output

This will create the below output −

Hello Beautiful World!

Example 4

In the following example, we are using the substr_replace() function for replacing a part of multiple strings in the given array.

<?php
   // Array of strings
   $strings = ["Hello World!", "Hello Tutorials point!", "Welcome to PHP World!"];
   
   // Replace "World" with "Everyone" in all strings
   $results = substr_replace($strings, "Everyone", 6, 5);

   print_r($results);
?> 

Output

Following is the output of the above code −

Array
(
   [0] => Hello Everyone!
   [1] => Hello Tutorials point!
   [2] => Welcome to PHP Everyone!
)
php_function_reference.htm
Advertisements