我有一个如下所示的段落
$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?
答案 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>';