【问题标题】:Ignore the punctuation and highlight the pattern in given string忽略标点符号并突出显示给定字符串中的模式
【发布时间】:2019-07-11 10:54:01
【问题描述】:

我有一个模型字符串和匹配模式列表。即使模式/模型中的任何单词包含标点符号,我也想突出显示给定模型字符串中的所有匹配模式。

示例字符串:

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.

模式列表: 1. printing and typesetting industry Lorem Ipsum 2. industry's standard dummy text ever since the 1500s, 3. type specimen book, It has survived 4. but also the leap into electronic typesetting, remaining essentially unchanged. 5. containing Lorem Ipsum passages and 6. PageMaker including versions of Lorem Ipsum.

预期输出:

我得到的输出:

问题:

这里 1,3,5 模式没有被突出显示。因为它们包含某种标点符号,但该单词的模型中不存在标点符号。

#1:在第一个模式中,单词industry 之后没有标点符号,而industry. 中有模型字符串。看来这两个词是不同的,所以这不是突出显示。但我希望它应该忽略标点符号并突出显示字符串。

#3:在第三种模式中,单词有不同的标点符号book,book.

我想突出显示字符串,即使模型或模式字符串中存在任何带有标点符号的单词。(如果不突出标点符号也可以,但应该突出显示单词)

我不希望模型字符串有任何变化,它应该与标点符号相同,只需突出显示匹配模式即可。

<?php
$model = '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.';
$phrases= [
    "printing and typesetting industry Lorem Ipsum"
    , "industry`s standard dummy text ever since the 1500s,"
    ,"type specimen book, It has survived"
    ,"but also the leap into electronic typesetting, remaining essentially unchanged."
    ,"containing Lorem Ipsum passages and"
    ,"PageMaker including versions of Lorem Ipsum."
];

$phrases = array_map(function($phrase) {
    return preg_replace('/\s+/', '\s+', '/(' . preg_quote($phrase, '/') . ')/iu');
}, array_reverse($phrases));

echo  $model = preg_replace($phrases, '<span style="color:red">$0</span>', $model);

工作示例

https://3v4l.org/QD8WY

【问题讨论】:

  • 为什么不在匹配短语之前的步骤中使用 preg_replace 去掉句号和逗号的例句?
  • preg_replace('/[.,!]/', '', $model) 会这样做吗? 3v4l.org/2pSZe ... 或者您希望在返回中包含标点符号?
  • @KIKOSoftware 是的所有标点符号(这些就足够了.?!,:;-{}[]()'`")。我已经处理了行尾,现在空格和行尾不是重要
  • @user3783243 exaclty 它应该包含在回报中。模型字符串应该与它相同。您的解决方案是正确的,但模型字符串中缺少标点符号
  • @HaydenEastwood 是的,同意但最终字符串应该有它不想从模型字符串中丢失标点符号,那么我们如何在输出字符串的末尾添加标点符号。

标签: php php-5.5


【解决方案1】:

您可以调整现有代码以忽略模型文本和短语之间的标点符号差异。您需要查找标点符号和空格,并将它们中的每一个与标点符号和/或空格匹配,而不是仅仅寻找匹配的空格。这段代码应该做你想做的:

$phrases= [
    "printing and typesetting industry Lorem Ipsum"
    , "industry`s standard dummy text ever since the 1500s,"
    ,"type specimen book, It has survived"
    ,"but also the leap into electronic typesetting, remaining essentially unchanged."
    ,"containing Lorem Ipsum passages and"
    ,"PageMaker including versions of Lorem Ipsum."
];
$phrases = array_map(function($phrase) {
    return preg_replace(array('/[.?!,:;\-{}\[\]()\'`"]/', '/\s+/'), 
                        array('([.?!,:;\\-{}\\[\\]()\'`"]|\s+)', '([.?!,:;\\-{}\\[\\]()\'`"]*\s+|\s+[.?!,:;\\-{}\\[\\]()\'`"]*)'), 
                        "@$phrase@iu");
}, array_reverse($phrases));

echo  $model = preg_replace($phrases, '<span style="color:red">$0</span>', $model);

输出:

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

Demo on 3v4l.org

【讨论】:

  • @Omi 有帮助吗?如果没有,您能否提供更多信息来解决问题?还是你已经解决了?如果是这样,您应该发布您的解决方案并接受它作为答案。
猜你喜欢
  • 2012-07-14
  • 1970-01-01
  • 1970-01-01
  • 2014-04-19
  • 2011-09-04
  • 1970-01-01
  • 2017-05-05
  • 1970-01-01
  • 2012-03-09
相关资源
最近更新 更多