joejoeso joejoeso - 4 months ago 21
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
[his family was far from rich]
and insert into a new line
right after
[his family was far from rich]


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', $string);