Home >Backend Development >PHP Tutorial >PHP replace
replace() function is a function in PHP which mainly deals with strings and aims in replacing any string by performing a search for the string that needs to get replaced. The pattern of search for string can be in a way where either the entire search string needs to be replaced or it can be array that can get replaced by the searched replacement string in given string or array. replace() function is a build-in function in PHP which returns a new string or array with replaced values. This function accepts four parameters or arguments mandatorily namely search_val, replace_val, subject_val and count.
ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock TestsStart Your Free Software Development Course
Web development, programming languages, Software testing & others
Given below is the syntax :
str_replace(search_val, replace_val, subject_val, count)
The syntax flow is in a way where the parameters are represented as follows:
replace() function in PHP is a build-in function which is used to replace all the possible parameters that need to get replace in the given string therefore it has some working criteria that needs to be followed:
Given below are the examples mentioned :
This program demonstrates the replace() function in PHP which first finds for the string and then replaces the value of the string with some part as defined. It makes the entire php string replaced with some value as shown in the output.
Code:
<!DOCTYPE html> <html> <body> <p>Let's Find for the string "Life_in_writing" and replace the value of writing with "Anusua"</p> <?php echo str_replace("writing","Anusua","Life_in_writing!"); ?> </body> </html>
Output
This program demonstrates an array of fruit which is trying to replace the particular value of the fruit like guava string with apple as shown in the output.
Code:
<!DOCTYPE html> <html> <body> <p>Find the array with fruit guava and then substitute it with another fruit Apple.</p> <?php $ar_ray = array("guava","kiwi","apple","orange"); print_r(str_replace("guava","apple",$ar_ray,$k)); echo "<br>" . "replaced_fruit: $k"; ?> </body> </html>
Output:
This program demonstrates the substitution of string with elements and values of String with some subject_val string as the string value is less it will get substituted easily as show shown in the output.
Code:
<!DOCTYPE html> <html> <body> <?php $search = array("Welcome","Everyone!"); $replace_str = array("Zee"); $arr_ay = array("Welcome","All",":)"); print_r(str_replace($search,$replace_str,$arr_ay)); ?> </body> </html>
Output:
This program demonstrates the ireplace_string() which acts as case insensitive when compared with the replace() function as shown in the output.
Code:
<!DOCTYPE html> <html> <body> <p>find the string "All is good" and then make it replaced with the capital letter EVERYONE!</p> <?php echo str_ireplace("EVERYONE!","Anusua","All is good"); ?> </body> </html>
Output:
PHP replace() is a function which gives programmers flexibility and scope of re-usability at the time of execution and lets user to use function for searching and replacing string accordingly. It gives users a view of implementation in terms of requirement when it comes to adopting the functionality. Overall a function which plays a significant role in PHP.
The above is the detailed content of PHP replace. For more information, please follow other related articles on the PHP Chinese website!