To replace a substring in a string in PHP, you can use the built-in str_replace()
function.
Syntax:
main.php42 chars2 lines
Parameters:
$search
- the substring to be replaced$replace
- the new substring to replace the old substring$subject
- the original stringExample:
main.php100 chars4 lines
Output:
main.php15 chars2 lines
In the example above, we're replacing the word "world" with "everyone" in the string "hello world".
Note that str_replace()
is case-sensitive, so if you want to replace all occurrences (case-insensitive) of the substring, you can use the str_ireplace()
function instead.
gistlibby LogSnag