Question #225   Submitted by Answiki on 10/12/2021 at 06:58:23 PM UTC

How to replace a substring in a string in PHP?

Answer   Submitted by Answiki on 11/23/2020 at 08:23:05 PM UTC

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;

3 events in history
Question by Answiki 10/12/2021 at 06:58:23 PM
How to replace a substring in a string in PHP?
Answer by Answiki on 11/23/2020 at 08:23:05 PM

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;

Question by Answiki 11/23/2020 at 08:16:10 PM
In PHP, how to replace a substring in a string?
# ID Query URL Count

Icons proudly provided by Friconix.