Replace a substring in PHP str_replace

2018-03-20


Don’t know why PHP’s official manual is so confusing for the str_replace function. Replace a substring.

Since most of time we just want to replace a substring from the original string, so here we just give a simple example:

//using str_replace to replace substring sample by codeease.com
 
$original_str = "This is original string which include old string";
 
str_replace("old string", "new string", $original_str);

You can also remove old string using an empty string to replace:

str_replace("old string", "", $original_str);

Note: str_replace is for PHP4, PHP5, PHP7 currently