$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 & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.