joejoeso joejoeso - 13 days ago 5
PHP Question

php how do I add sentence after content in string is found?

Suppose I have this string.

$string = "While it has long been disputed,
it's estimated that Julius Caesar was born in
Rome on July 12 or 13, 100 BC.
He hailed from Roman aristocrats[his family was far from rich].
When Caesar was 16 his father, Gaius Caesar, died.
He remained close to his mother, Aurelia."


I want to search
$string
for
[his family was far from rich]
and insert into a new line
from www.wiki.com
right after
[his family was far from rich]
.

Answer

If the content is static and you want to replace every occurrence use str_replace.

$search = '[his family was far from rich]';
echo str_replace($search, "\n" . $search . 'from www.wiki.com', $string);
Comments