匹配两篇文章并突出显示匹配的数据

时间:2013-05-03 18:08:56

标签: php regex string match highlight

我有两篇文章(纯文本),我希望将第一篇文章与第二篇文章相匹配,并突出显示匹配的数据。

情景:

ARTICLE(A)
Lorem Ipsum只是印刷和排版行业的虚拟文本。自16世纪以来,Lorem Ipsum一直是业界标准的虚拟文本,当时一台未知的打印机采用了类型的厨房,并将其拼凑成一本类型的样本。它不仅存在了五个世纪,而且还延续了电子排版,基本保持不变。它在20世纪60年代随着包含Lorem Ipsum passahttp://stackoverflow.com/posts/16365110/editges的Letraset工作表的推出而流行,最近推出了包括Lorem Ipsum版本在内的Aldus PageMaker等桌面出版软件。

ARTICLE(B)
自16世纪以来的虚拟文本。它不仅存活了五个世纪。它在20世纪60年代随着包含Lorem Ipsum段落的Letraset板材的推出而普及

输出应该是这样的
Lorem Ipsum只是印刷和排版行业的虚拟文本。 Lorem Ipsum自16世纪以来一直是业界标准的<span class="highlight">虚拟文本</span>,当一个未知的打印机拿出一个类型的厨房并拼写它来制作一个类型的样本书。 <span class="highlight">它不仅存在了五个世纪</span>,而且还延续了电子排版,基本保持不变。 <span class="highlight">它在20世纪60年代推出了包含Lorem Ipsum段落{4}的Letraset表格,最近发布了包括Lorem Ipsum版本在内的桌面出版软件Aldus PageMaker。

注意:
Aricle B可以被视为像这样的数组

</span>

如何实现上述输出?

我希望这很清楚。 提前谢谢。

1 个答案:

答案 0 :(得分:0)

使用程式化文本在A中定期进行str_replace篇文章。

<?
$a = "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.";

$b = array(
 "dummy text ever since the 1500s",
 "It has survived not only five centuries",
 "It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages"
);

$h = "";
foreach($b as $btext)
{
    $a = str_replace($btext,"<span class='highlight'>$btext</span>",$a);
}
echo $a;
?>