PHP str_replace for string replacing

Created At: 2022-04-05 02:16:24 Updated At: 2022-04-08 19:34:06


It's your best friend when you have a string, and wanna replace part of the string with other things or other strings or anything.

It takes three arguments

str_replace("word to look for", "word to replace with", "original string")

Example 1

$str = "hello world";
$newStr = str_replace("world", "dbestech", $str);

This would print

hello dbestech


Example 2

Another good exmple when you do url manupulatation for seo in PHP or Laravel. You wanna replace the white space with "-". So str_replace() comes to rescue you.

$url = "the best backend framework";
$newUrl = str_replace(" ", "-", $url);

This would print somethng like below



Add Reviews

Recommended Posts

Latest Posts

Subscribe our newsletter