【问题标题】:Match two articles and highligt matched data匹配两篇文章并突出显示匹配的数据
【发布时间】:2013-04-28 05:21:09
【问题描述】:

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

场景:

文章(A)
Lorem Ipsum 只是印刷和排版行业的虚拟文本。自 1500 年代以来,Lorem Ipsum 一直是行业的标准虚拟文本,当时一位不知名的印刷商采用了一种类型的厨房并将其加扰以制作类型样本书。它不仅经历了五个世纪,而且经历了电子排版的飞跃,基本保持不变。它在 1960 年代随着包含 Lorem Ipsum passahttp://stackoverflow.com/posts/16365110/editges 的 Letraset 工作表的发布而得到普及,最近又随着包括 Lorem Ipsum 版本的 Aldus PageMaker 等桌面出版软件的发布而普及。

文章(B)
自 1500 年代以来的虚拟文本。它不仅存在了五个世纪。它在 1960 年代随着包含 Lorem Ipsum 段落的 Letraset 表的发布而普及

输出应该是这样的
Lorem Ipsum 只是印刷和排版行业的虚拟文本。自从 1500s</span> 以来,Lorem Ipsum 一直是行业标准的<span class="highlight">dummy 文本,当时一位不知名的印刷商拿走了一个类型的厨房,并把它加扰到制作一个类型样本簿。 <span class="highlight">它不仅经历了五个世纪</span>,而且还跨越了电子排版,基本保持不变。 <span class="highlight">它在 1960 年代随着包含 Lorem Ipsum 段落</span> 的 Letraset 工作表的发布而流行起来,最近还有像 Aldus PageMaker 这样的桌面出版软件,包括 Lorem Ipsum 的版本。

注意:
B条可以这样看成一个数组

[0]=dummy text ever since the 1500s
[1]=It has survived not only five centuries
[2]=It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages

如何实现以上输出?

我希望这已经足够清楚了。 提前致谢。

【问题讨论】:

  • 你有没有尝试过实现任何东西?
  • 他想展示从第一个到第二个添加了什么。因此,如果第二个版本的文本不同,它将被环绕。
  • 如何知道文章 A 中应该匹配多少文章 B?例如:文章 B 有dummy text ever since the 1500s。如果该句子出现在文章 A 中,它是否应该单独匹配 dummy text rest is useless 中的单词 dummy text
  • 自 1500 年代以来,您可以将文章 B 视为数组 [0]=虚拟文本。 [1]=它不仅存在了五个世纪。 [2]=它在 1960 年代随着包含 Lorem Ipsum 段落的 Letraset 表的发布而普及

标签: php regex string match highlight


【解决方案1】:

只需在 A 中的文章 B 中使用程式化文本进行常规 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;
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-21
    • 2012-11-03
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    相关资源
    最近更新 更多