我有一个模型字符串和匹配模式列表。我想突出显示给定模型字符串中的所有匹配模式,即使模式/模型中的任何单词都包含标点符号。
示例字符串:
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);
工作示例:
答案 0 :(得分:0)
您可以调整现有代码,以忽略模型文本和短语之间的标点符号差异。您不仅要查找匹配的空格,还需要查找标点符号和空格,并将它们与标点符号和/或空格进行匹配。此代码应执行您想要的操作:
$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>