PHP String Replace

PHP String Replace function will be used for replacing all occurrences of a string with in a string. In str_replace, it expects three mandatory arguments first argument is search word, second argument is replace word, third argument is the main string where you want to replace it a term. Fourth argument is not mandatory it’s count, if you want to limit number of replacement performed

, you can use this argument if you are performing string replace on a array.

PHP String Replace syntax

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

$search – it’s the word for searching in string, mandatory argument.
$replace – $search word will be replaced by $replace, mandatory argument.
$string – $sting it’s main string where we will search.
$count – variable that counts number of replacements.

Simple code snippet for PHP String Replace

Simple sting replace code for replacing dear in place of world, output will be Hello Dear.

$string = "Hello world";
$find = "world";
$replace = "Dear";
echo str_replace($find,$replace,$string);

Instead of just string here I’m using an array to replace it with search term.

$arr = array("blue","red","green","yellow");
echo "Replacements: $i";

Check Your Website Rankings

Vivek Vengala

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 0 comments

Leave a Reply: