Ignore the punctuation and highlight the pattern in given string

北城余情 提交于 2019-12-02 11:16:13

You can adapt your existing code to ignore punctuation differences between the model text and the phrases. Instead of just looking for matching spaces, you need to look for punctuation and spaces, and match each of them against punctuation and/or a space. This code should do what you want:

$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);

Output:

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!