Question #373

In PHP, how to replace a substring in a string?

Answer

The PHP function str_replace($substring, $replace, $string) returns the string $string with all substrings $substring replaced by $replace. Here is an example :

// Original string
$phrase = "To be or not to be.";
// Replace 'be' by 'eat' in the string
$newPhrase = str_replace('be', 'eat', $phrase);
// Displays: To eat or not to eat.
echo $newPhrase;

# ID Query URL Count

Icons proudly provided by Friconix.