HOW TO USE THE str_replace() FUNCTION IN PHP
The str_replace() function is a handy little function that allows you to replace certain parts of a string with something else. It takes three parameters: what to replace, the new replacement and the actual string to perform these actions on.
Example 1:
$string="John is my name";
$newstring=str_replace("is", "was", $string);
echo $newstring;
The above will output: John was my name
Here we have took the string $string and replaced the word is with the word was.
Example 2:
Another way to do the same thing:
$find="mexico";
$replace_with="New York";
$string="once upon a time in mexico";
$newstring=str_replace($find, $replace_with, $string);
echo $newstring;
The above will output: once upon a time in New York
The word mexico is replaced with New York.
Note that you can also use this function on any single or multiple characters and not just words.
|