PHP str_replace() Function

Syntax : str_replace ( $find, $replace, $string, $count)

str_replace() is a built-in function which is used for replacing some characters with some other characters in a string. i.e replaces all the occurrences of a word within the string

The str_replace() function replaces some characters with some other characters in a string.

Example 1 :

 

<?php

$search = "Tutorial"; 

$replace = "Guide"; 

$string = "Welcome to My Web Aone Tutorial"; 

str_replace($search, $replace, $string); 

//output : Welcome to My Web Aone Guide 

?>

Example 2 :

 

<?php

$string  = "We should eat vegetables every day in the morning"; 

$find = array("eat", "vegetables", "morning");  

$replace = array("drink", "milk", "evening"); 

$phrase = str_replace($string, $find, $replace);  

//output :  We should drink milk every day in the evening. 

?>

Example 3 :

Replace multiple characters with corresponding multiple characters

 

<?php 

$string = "one three five one seven two nine"; 

$search = array("one", "two", "three"); 

$replace = array("four", "five", "six"); 

echo str_replace($search, $replace, $string); 

//output :  "four six five four seven five nine"; 

?>