Case sensitive String Replace

Replace literal text in a string

str_replace() performs case-sensitive literal replacement. Its optional fourth argument receives the number of replacements, which is useful when you need to know whether anything changed.

$text = 'red car, red bike';
$result = str_replace('red', 'blue', $text, $count);
echo $result; // blue car, blue bike
echo $count;  // 2

The examples below show single-string replacement, array-based replacement, highlighted output and the optional replacement count.

$str='Hello to plus2net.com';
$search='Hello';
$replace='Welcome';
echo str_replace($search,$replace,$str);
Output is here
Welcome to plus2net.com
Syntax
str_replace(search,replace,main_string,count);
search:
Required: String to be searched for matching, can be an array
replace:
Required: String to be replaced if found using search string, can be an array
main_string:
Required : String on which search and replace to be applied, can be an array
count:
Optional: Stores the number of replacements performed.


Output is a string or array after performing search and replace over the input string

To replace a part of the string with another string we will use str_replace function in PHP. This function will replace all the occurrence of the string we target and will replace with the string we supply.
Here is an example.
echo $string;
echo "<br><br>";
$string = str_replace("html", "PHP", $string);
echo "<b>After replace</b> <br><br>$string";
Running the code above produces the following result


This is an example on how html is used in developing user-friendly web sites. By
using HTML we can control the content of a page

After replace

This is an example on how PHP is used in developing user-friendly web sites. By
using HTML we can control the content of a page

The lowercase text html is replaced by PHP, while uppercase HTML is unchanged.
str_replace() is case-sensitive. Use str_ireplace() for case-insensitive replacement.

Find & Replace

You can pass an array of search strings and a matching array of replacement strings. Each search value is replaced by the value at the same position in the replacement array. Original String
Welcome to Plus2net.com to learn PHP, MYSQL, HTML, jquery and many more.
After replacement
Welcome to plus2net.com to learn php, MySQL, html, JQuery and many more.
Here is the code
$contents = "Welcome to Plus2net.com to learn PHP, MYSQL, HTML, jquery and many more.";
echo "Original String $contents";
$search = array("Plus2net.com", "PHP", "jquery", "HTML", "MYSQL");
$replace = array("plus2net.com", "php", "JQuery", "html", "MySQL");
$contents = str_replace($search, $replace, $contents);
echo "After replacement $contents";

Highlighting the changes.

Replacement text can include HTML markup when the output is intentionally rendered as HTML. Escape untrusted values before including them in HTML.
$replace=array("plus2net.com","php","<span style='background-color: #FFFF00'>JQuery</span>","html","MySQL");

Using optional parameter count

The optional fourth argument stores the number of replacements performed by str_replace().
$str='Raju is a good boy and he has many good habits.';
$search='good';
$replace='bad';
echo str_replace($search, $replace, $str,$count);
echo "<br>Number of replacements = $count <br>"; 
Output is here
Raju is a bad boy and he has many bad habits.
Number of replacements = 2
Read how search & replace is used to change content of a page
String Functions Case in-sensitive string replace


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer