Example 1 — Replace Part of a String
Swap "World" for "PHP".
<?php
$text = "Hello World";
$new = substr_replace($text, "PHP", 6, 5);
echo $new, PHP_EOL; How It Works
- Start at 6. That is where "World" begins.
- Replace 5 characters. "World" has five letters.
- Insert new text.
"PHP"takes their place. - Print. You get
Hello PHP.

