Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php如何在找到字符串中的内容后添加句子?_Php_Str Replace - Fatal编程技术网

php如何在找到字符串中的内容后添加句子?

php如何在找到字符串中的内容后添加句子?,php,str-replace,Php,Str Replace,假设我有这根绳子 $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.

假设我有这根绳子

$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
中搜索
[他的家庭远不富裕]
,并在
[他的家庭远不富裕]
之后插入www.wiki.com中的新行
,如果内容是静态的,并且您希望替换每次使用


也许你想要这样的东西


$string=“尽管这一问题长期以来一直存在争议,
据估计,朱利叶斯·凯撒出生于年
公元前100年7月12日或13日的罗马。
他出身于罗马贵族[他的家族远非富有]。
凯撒16岁时,他的父亲盖乌斯·凯撒去世。
他和母亲奥雷莉亚保持着亲密的关系。”

$tofind=“[他的家庭远非富有]”

$newstring=str\u replace($tofind,$tofind.“from www.wiki.com”,$string)

echo$newstring


这不是没有新行的我的答案吗?好的,你可以像这样在“from www.wiki.com”中添加标签:$newstring=str\u replace($tofind,$tofind.
from www.wiki.com,$string);
$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.";

$tofind="[his family was far from rich]";

$newstring= str_replace($tofind,$tofind." from www.wiki.com",$string);

echo $newstring;