$string=" Welcome to html & HTML section";
$string=preg_replace("/html/","MySQL",$string);
echo $string;Output
Welcome to MySQL & HTML section
preg_replace('search_string','replace_string','main_string',limit,count);
Search_string:Enter the pattern of search string. This can be an array. <?Php
$string=" Welcome to plus2et.com , you can read html, php , jquery & HTML here";
$string=preg_replace("/html/","MySQL",$string);
echo $string;
?>
Output is here
Welcome to plus2et.com , you can read MySQL, php , jquery & HTML here
Note that we could change only html , not HTML . This is a case sensitive search and replace. To make it case in sensitive we have to add one i after the string delimiters.
$string=preg_replace("/html/i","MySQL",$string);
Now it is not case sensitive and both occurrence of the string HTML & html will be replaced with MySQL
<?Php
$string=" Welcome to plus2et.com , you can read html, php , jquery & HTML here";
$string=preg_replace("#html#i","MySQL",$string,-1,$count);
echo $string;
echo "<br>Total replacement done : $count ";
?>
Output is here
Welcome to plus2et.com , you can read MySQL, php , jquery & MySQL here
Total replacement done : 2
If you are dynamically building a query by taking different conditions connected by AND operator then this code is useful as you can replace the first occurrence of AND with a WHERE to complete your query.
$sql=preg_replace("/AND/","WHERE",$sql,1);
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.