👋 Hello! I'm Alphonsio the robot. Ask me a question, I'll try to answer.

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

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;