preg_replace与数组混合和匹配单词组合

时间:2015-06-11 00:46:06

标签: php regex

我想要从两个数组中加粗彼此相邻(或相隔一个单词)的关键字。我正在采用这种方法,因为一个巨大的关键字列表似乎正在消耗内存。这个似乎效率更高。

$firstarray= array('baseball','soccer','tennis');
$secondarray= array('ball','shoes','glove');

示例:

$string = She like to play soccer and baseball. But in the evening she likes to hit the tennis ball. She also just bought new tennis court shoes. Tennis court performance shoes.

我想找的针是“网球”和“网球鞋”。您会注意到COURT不在数组中,但它的BETWEEN有两个关键字,我想包含它。 “网球场表演”鞋不是针,因为两个关键词被另外两个非关键词分开。

不会是一针。

最终:

 $string = preg_replace("#\b(?:(firstarray)\W+(?:\w+\W+){0,2}?(secondarray)\b#i", '<strong>tennis ball</strong><strong>tennis court shoes</strong>', $string);
  1. 显然问题是,我很可怕,在正则表达式上很糟糕。我找不到许多带有正则表达式的解决方案,这些解决方案适用于列表/数组的组合。
  2. 我不确定我将如何使用两个数组,混合和匹配,并将它们传递到大海捞针。

1 个答案:

答案 0 :(得分:1)

这个怎么样?

<?php
$firstarray= array('baseball','soccer','tennis');
$secondarray= array('ball','shoes','glove');
$string = 'She like to play soccer and baseball. But in the evening she likes to hit the tennis ball. She also just bought new tennis court shoes. Tennis court performance shoes.';
foreach($firstarray as $term1) {
    foreach($secondarray as $term2) {
        $string = preg_replace('~\b(' . preg_quote($term1) . '\b\s+([a-zA-Z]+\s+)?\b' . preg_quote($term2) . '\b)~', '<strong>$1</strong>', $string); 
    }
}
echo $string;

输出:

She like to play soccer and baseball. But in the evening she likes to hit the <strong>tennis ball</strong>. She also just bought new <strong>tennis court shoes</strong>. Tennis court performance shoes.

现场直播:http://sandbox.onlinephpfunctions.com/code/e8b34064f235933b5a5805bbe420d7d44d00ee46

我们遍历两个数组以遍历每个可能的术语组合。我们在每个配对上运行正则表达式并在找到匹配项时替换它。 ([a-zA-Z]+\s+)是我在单词列表之间找到一个可能的单词的方式。您可能希望在&#34; word&#34;中添加连字符或您想要允许的任何其他字符。我定义了一个&#34;字&#34;以白色空间结束。

这也是区分大小写的,只是注意到你有i修饰符。如果您想重新启用,只需添加该内容并取出A-ZA-Z赢得i的任何内容,只需多余的内容。

preg_quote目前不是必需的,但如果您将来的条款中有特殊的正则表达式字符,那么这是必要的;最好立即包括它。