Categories: PHP

str_replace – PHP String Functions

Syntax :

str_replace ( find, replace, string, count );

Description :

str_replace() function will replace all occurrences of the search string with the replacement string. It is case – sensitive function.

This function works by the following rules:

  • If the string to be searched is an array, it returns an array.
  • If the string to be searched is an array, find and replace is performed with every array element.
  • If both find and replace are arrays, and replace has fewer elements than find, an empty string will be used as replace.
  • If find is an array and replace is a string, the replace string will be used for every find value.
Note : This function is binary-safe.
Note : This function is case-sensitive. Use the str_ireplace() function to perform a case-insensitive search. If you don’t need replacing rules like regular expressions, you should always use this function instead of preg_replace().

Parameter :

  • find – This is a Required parameter. It sets values to find.
  • replace – This is a Required parameter. It sets values to replace.
  • string – This is a Required parameter. It sets string to search.
  • count – This is an Optional parameter. If passed, this will be set to the number of replacements performed.

Output :

This will return string or an array with replaced values.


ChangeLog :

[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
Version, Description
PHP 5.0 , The count parameter was added.
PHP 4.0.5, From this version most of the parameters can be an array.

[/table]


Related articles : str_ireplace(), preg_replace, substr_replace().


str_replace() – PHP Functions Example 1 :
<?php
// below statement will replace the Hi word with Hello."
$strExample = "Hi from tutorialmines!"; 
echo str_replace("Hi","Hello",$strExample);
?>

In above example ,We have a string “Hi from tutorialmines! “. Now this function will replace the value “Hi” with “Hello” value in the string. Output of above code in the browser is as below:

Hello from tutorialmines!

str_replace() – PHP Functions Example 2 :
<?php
$arr = array("january","febuary","march","april");
print_r(str_replace("febuary","Month-2",$arr,$i)); // This function is case-sensitive
echo "Replacements: $i";
?>

In above example ,We have an array of strings “january”,”febuary”,”march”,”april”. Now this function will replace the value in array “Febuary” with “Month-2” value in the string. Output of above code in the browser is as below:

Array ( [0] => january [1] => Month-2 [2] => march [3] => april ) Replacements: 1

str_replace() – PHP Functions Example 3 :  less elements in replace than find
<?php
$strArr = array("january","febuary","march","april");
$find = array("january","febuary",""); // Case-sensitive
$replace = array("first replacement","second replacement");
print_r(str_replace($find,$replace,$strArr,$count));
echo "<br/>Numbers of replacements done are - ".$count;
?>

In above example ,We have an array of strings “january”,”febuary”,”march”,”april”.

Now this function will find the value in array “januaRy”,”Febuary”,””. and replace them with “”first replacement”,”second replacement”” values. Output of above code in the browser is as below:

Array ( [0] => first replacement [1] => second replacement [2] => march [3] => april )
Numbers of replacements done are – 2


jyoti rani

Recent Posts

Modern Toys, Magical Moments: Why the Best Toy Shop in Noida Is More Than Just a Store

When it comes to children, there’s one universal truth: the right toy can spark imagination, build skills, and make memories…

6 months ago

Rediscovering Joy: A New Era of Creativity & Comfort in Toy Stores

In today’s digital age, where screens and gadgets dominate our children’s lives, there’s something heartwarming about a well-loved plush toy…

7 months ago

Unboxing Imagination: Discovering the Joy of Play at a Toy Store in Noida

In a world dominated by screens and fast-paced routines, it’s easy to forget the simple magic of a toy in…

7 months ago

Imagination Unboxed: Discover Joy at the Toy Shop in Delhi

In the heart of Delhi’s vibrant streets lies a world where imagination meets innovation — the magical universe of toys.…

7 months ago

Play with Purpose: Discovering the Ultimate Toy Store in Noida

When was the last time a toy truly amazed you—not just as a product, but as a thoughtful tool for…

7 months ago

From Tears to Toys: Exploring Modern Childhood through Delhi’s Favorite Toy Shop

In the digital age, the way we experience childhood has changed, but the essence remains the same—imagination, exploration, and joy.…

7 months ago