Example 1 — Replace One Word
The most common use: swap one piece of text for another.
<?php
$text = "Hello world";
$new = str_replace("world", "PHP", $text);
echo $new, PHP_EOL; How It Works
- Store the original text.
- Pass the search value, the replacement, and the subject.
- PHP replaces every match of "world".
- The original $text stays unchanged; the new string is returned.

