Open In App

PHP substr_replace() Function

Last Updated : 22 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report


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



Previous Article
Next Article

Similar Reads

How to get the function name inside a function in PHP ?
To get the function name inside the PHP function we need to use Magic constants(__FUNCTION__). Magic constants: Magic constants are the predefined constants in PHP which is used on the basis of their use. These constants are starts and end with a double underscore (__). These constants are created by various extensions. Syntax: $string = __FUNCTION
1 min read
How the User-Defined Function differs from Built-in Function in PHP ?
In PHP, functions are blocks of reusable code that perform specific tasks. They enhance code readability, modularity, and maintainability by encapsulating logic into named units. PHP functions can be categorized into two main types: user-defined functions and built-in functions. User-Defined FunctionsUser-defined functions are functions created by
2 min read
PHP 5 vs PHP 7
PHP is a server side scripting language designed for web development by Rasmus Lerdorf in 1994. Since its launch in 1994 PHP has become an industry standard supporting almost 80% of the websites ( 79.8% to be precise) with its closest competitor being ASP.Net at 19.8% and others like Ruby, Java trailing far behind. The PHP development team released
4 min read
PHP | Get PHP configuration information using phpinfo()
PHP provides us with a built-in function phpinfo() which gives us the details about the PHP version and PHP configuration of PHP installed in our system. To know about the Configurations and PHP version which is installed in your computer, a simple PHP script can be used. The script consists of a PHP function called "phpinfo()" which outputs inform
2 min read
PHP | php.ini File Configuration
At the time of PHP installation, php.ini is a special file provided as a default configuration file. It's very essential configuration file which controls, what a user can or cannot do with the website. Each time PHP is initialized, the php.ini file is read by the system. Sometimes you need to change the behavior of PHP at runtime, then this config
5 min read
How to import config.php file in a PHP script ?
The include statement in PHP copies the code of text from the file mentioned, into the file that uses the include statement. It directs the preprocessor to insert the content specified into the following program. The name of the file to be included is written in double-quotes. It is a good practice to write the basic database details and user detai
2 min read
How to include content of a PHP file into another PHP file ?
Including the content of a PHP file into another file reduces the complexity of code by reducing the code and improving its modularity by splitting the code into different files so that it is easy to understand and manage. There are two ways to do it by using the following PHP functions. PHP include() Function: It will show the warning if the file
2 min read
PHP | imagecreatetruecolor() Function
The imagecreatetruecolor() function is an inbuilt function in PHP which is used to create a new true-color image. This function returns a blank image of the given size. Syntax: resource imagecreatetruecolor( $width, $height ) Parameters: This function accepts two parameters as mentioned above and described below: $width: This parameter is used to s
2 min read
PHP fpassthru( ) Function
The fpassthru() function in PHP is an inbuilt function which is used to read data from a current position from a specified file until end of file and then write the result to the output buffer. The file which has to be read is sent as a parameter to the fpassthru() function and it returns the number of characters passed on success or FALSE on failu
2 min read
PHP | ImagickDraw getTextAlignment() Function
The ImagickDraw::getTextAlignment() function is an inbuilt function in PHP which is used to get the alignment applied when annotating with text. It helps to format the text by making it stick to left, right or middle. Syntax: int ImagickDraw::getTextAlignment( void ) Parameters: This function doesn’t accept any parameter. Return Value: This functio
2 min read
Article Tags :