Skip to content
PHP Logo

How can PHP be used to replace text within a variable?

To replace text within a variable in PHP, you can use the str_replace() function. This function takes three parameters: the text to find, the text to replace it with, and the original string. It then returns a new string with the replacements made.

php
$originalString = "Hello, World!";
$replacementString = str_replace("World", "PHP", $originalString);
echo $replacementString; // Output: Hello, PHP!