[英]Inject Text inside a paragraph
I have a paragraph like below我有一个像下面这样的段落
$content = '<p>Clashes broke out at one refinery on Tuesday when police broke up a blockade in Marseille. Workers at a large oil terminal in the port of Le Havre were due to go on strike on Thursday to block imports.Transport Minister Alain Vidalies said 40% of petrol stations around Paris were struggling to get fuel.Motorists have been panic-buying to avoid shortages.</p>';
I would like to inject another text $added_text = 'Honorable';
我想注入另一个文本
$added_text = 'Honorable';
before Transport Minister Alain Vidalies of this paragraph.交通部长阿兰·维达利斯 (Alain Vidalies)之前的这段话。
How can I do that using PHP ??我怎样才能使用 PHP 做到这一点?
You can achive it using strpos()
and substr_replace
as您可以使用
strpos()
和substr_replace
作为
$content = '<p>Clashes broke out at one refinery on Tuesday when police broke up a blockade in Marseille. Workers at a large oil terminal in the port of Le Havre were due to go on strike on Thursday to block imports.Transport Minister Alain Vidalies said 40% of petrol stations around Paris were struggling to get fuel.Motorists have been panic-buying to avoid shortages.</p>';
$find="Transport Minister Alain Vidalies";
echo $pos=strpos($content, $find);
$added_text = 'Honorable ';
echo $newstr = substr_replace($content,$added_text,$pos, 0);
You can glue strings to getting using .
您可以将字符串粘合到使用
.
, like this: , 像这样:
$content = '<p>Clashes broke out at one refinery on
Tuesday when police broke up a blockade in Marseille.
Workers at a large oil terminal in the port of
Le Havre were due to go on strike on Thursday to
block imports.'.$added_text.'Transport Minister Alain
Vidalies said 40% of petrol stations around Paris were
struggling to get fuel.Motorists have been panic-buying
to avoid shortages.</p>';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.