Tutorialspoint.dev

PHP | substr_replace() Function


The substr_replace() function is a built-in function in PHP and is used to replace a part of a string with another string. The index in the original string from which the replacement is to be performed needs to be passed as a parameter. If desired, the length up to which the replacement is to be done can also be specified. An array of strings can be provided as a parameter to this function, in which case the replacements will occur on each string in turn.

Syntax :

substr_replace($string, $replacement, $start, $length)

Parameters: This function accepts four parameters as shown in the above syntax out of which first three are mandatory and the last one is optional. All of these parameters are described below:

  • $string : This parameter is mandatory. It specifies the input string in which the replacement is to be made.
  • $replacement : This parameter is also mandatory. It specifies the string to be inserted in $string.
  • $start : This parameter is also mandatory. It specifies the position from which the replacement needs to be initiated.
    • If $start is a positive number, replacement starts at the specified position in the string
    • If $start is a negative number, replacement starts at the specified position from the end of the string
    • If $start is 0, replacement occurs from the first character of the string
  • $length : This parameter is optional. It specifies how many characters should be replaced. In case $length is not specified, the replacement stops at the end of $string
    • If $length is positive, it represents the length of the portion of $string which is to be replaced.
    • If $length is negative, it represents the number of characters from the end of $string before which the replacement needs to be stopped.
    • If $length is 0, insertion is done instead of replacement.

Return Value: The string generated after replacement is returned. In case of an array of strings, the array is returned.

Examples:



Input : $string = "Geeks for Geeks", $replacement = "GFG", $start = 0
Output : GFG

Input : $string = "Hello World", $replacement = "Hello", $start = 6
Output : Hello Hello

Below programs illustrate the substr_replace() function:

Program 1: In this program we will use the substr_replace() function without any $length parameter. All the characters from $start to the end of $string will get replaced by $replacement.

<?php
  
echo substr_replace("Hello World", "GFG", 6);
  
?>

Output

Hello GFG

Program 2: In this program we will use the substr_replace() function with $length set to 0. In this case, insertion will occur. No replacement will take place.

<?php
  
echo substr_replace("Contribute GFG", "to ", 11, 0);
  
?>

Output

Contribute to GFG

Program 3: In this program we will use the substr_replace() function with $length set to a positive value. In this case, the $replacement string will replace characters of $string up to $length from $start.

<?php
  
echo substr_replace("alone", "ph", 0, 2);
  
?>

Output

phone

Program 4: In this program we will use the substr_replace() function with $length set to a negative value. In this case, the $replacement string will replace characters of $string from $start and stop before $length number of characters from the end of the string .

<?php
  
echo substr_replace("alone", "ph", 0, -3);
  
?>

Output

phone

Program 5: In this program we will use the substr_replace() function without any $length parameter and $start set to a negative value. The replacement will start at the specified position from the end of the string.

<?php
  
echo substr_replace("alpha", "one", -3);
  
?>

Output

alone

Reference : http://php.net/manual/en/function.substr-replace.php



This article is attributed to GeeksforGeeks.org

You Might Also Like

leave a comment

code

0 Comments

load comments

Subscribe to Our Newsletter