在段落中注入文本

时间:2016-05-26 07:21:14

标签: php

我有一个如下所示的段落

$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>';

我想在本段交通部长Alain Vidalies 之前注入另一个文本$added_text = 'Honorable';

我怎样才能使用PHP?

2 个答案:

答案 0 :(得分:1)

您可以使用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);

检查 DEMO

答案 1 :(得分:0)

您可以使用.来粘合字符串,如下所示:

$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>';