PHP Substr_replace () function

PHP | substr_replace

Syntax:
substr_replace ($string, $replacement, $start, $length)
Parameters:This the function takes four parameters as shown in the above syntax, of which the first three are required and the last - optional. All of these parameters are described below:
  • $string:This parameter is required. It specifies the input string to be replaced.
  • $replace:this parameter is also required. It specifies the string to be inserted into $string.
  • $start:this parameter is also required. It indicates the position at which the replacement should start.
    • If $start is a positive number , the replacement starts at the specified position on the line
    • If $start is negative number , the replacement starts at the specified position from the endof the line
    • If $start is 0, the replacement occurs from the first character of the line
    • $length:This parameter is optional. Indicates how many characters to replace. If no length is specified, replacement stops at the end of the string
      • If $length is positive , it represents the length of the portion of $string to be replaced.
      • If $length is negative , it represents the number of characters from the end of the $line to which replacement should stop.
      • If $length is 0, insertion is performed instead of replacement.
      Returned value:string generated after replacement. In the 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
      The following programs illustrate the substr_replace() function:Program 1:In this program, we will use the substr_replace() function without the $length parameter. All characters from $start to the end of $string will be replaced with $replace. echo substr_replace ( " Hello World " , " GFG " , 6) ;  
      ?>
      Exit
      Hello GFG
      Program 2:In this program we will use substr_replace() with $length set to 0. In this case, an insert will occur. The replacement will not take place. echo substr_replace ( "Contribute GFG" , "to" , 11, 0);  
      ?>
      Exit
      Contribute to GFG 
      Program 3:In this program, we will use the substr_replace() function with a positive $length. In this case, the string $replace will replace the characters in the string $up to $length from the beginning of $. echo substr_replace ( "alone" , "ph" , 0, 2);  
      ?>
      Exit
      phone  Program 4:In this program, we will use substr_replace() with a negative $length. In this case, the $replace string will replace the $string from $start and stop up to $length from the end of the string.      echo  substr_replace (  "alone" ,  "ph" , 0, -3);  
      ?>
      Exit
      phone  Program 5:In this program, we will use the substr_replace() function without any of the $length and $start parameters set to a negative value. Replacement will start at the specified position from the end of the line.       echo  substr_replace (  "alpha" ,  "one" , -3);  
      ?>
      Exit
      alone  Link: http://php.net /manual/en/function.substr-replace.php 



PHP Substr_replace () function: StackOverflow Questions