php 找到字符串中的内容后如何添加句子?
php how do I add sentence after content in string is found?
假设我有这个字符串。
$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."
我想在 $string
中搜索 [his family was far from rich]
并在 [his family was far from rich]
之后插入一个新行 from www.wiki.com
。
如果内容是静态的并且您想替换每个出现的地方,请使用 str_replace
。
$search = '[his family was far from rich]';
echo str_replace($search, "\n" . $search . 'from www.wiki.com', $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.";</p>
<p>$tofind="[his family was far from rich]";</p>
<p>$newstring= str_replace($tofind,$tofind." from www.wiki.com",$string);</p>
<p>echo $newstring;</p>
<p></pre>
假设我有这个字符串。
$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."
我想在 $string
中搜索 [his family was far from rich]
并在 [his family was far from rich]
之后插入一个新行 from www.wiki.com
。
如果内容是静态的并且您想替换每个出现的地方,请使用 str_replace
。
$search = '[his family was far from rich]';
echo str_replace($search, "\n" . $search . 'from www.wiki.com', $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.";</p>
<p>$tofind="[his family was far from rich]";</p>
<p>$newstring= str_replace($tofind,$tofind." from www.wiki.com",$string);</p>
<p>echo $newstring;</p>
<p></pre>