How to replace the part of a string with another string in PHP

The str_replace() function is an built-in function in PHP which is used to replaces any portion of a string with another string. This function is binary-safe.

Note:

This function is case-sensitive. You can use the str_ireplace() function for case-insensitive search.

Syntax:

str_replace(find,replace,string,count)

find – This parameter is required. It specifies the value to find.

replace – This parameter is required. It specifies the value to replace the value in find.

string – This parameter is required. It specifies the string to be searched.

count – This parameter is optional. A variable that counts the number of replacements.

Example:

<?php
$str = "Welcome to PHP Tutorials!";

// Display replaced string
echo str_replace("PHP", "MYSQL", $str); //Output:Welcome to MYSQL Tutorials!
?>

 

Praveen Kumar

I am a fullstack web developer with a passion for SEO, creating stunning websites, and organic marketing. I am a perfectionist.

Post navigation