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


preg_replace mix and match word combinations with array

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

$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 不在数组中,但它在两个关键字之间,我希望包含它。"网球场性能"鞋不是一根针,因为两个关键字被另外两个非关键字分开。

不会是一根针。

所以最终:

 $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. 不确定我究竟如何使用两个数组,混合和匹配,并将它们传递到大海捞针中。

这个怎么样?

<?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+)是我在单词列表之间找到一个可能的单词的方法。您可能希望在"单词"中添加连字符或要允许的任何其他字符。 我将"单词"定义为以空格结尾。

此外,这区分大小写,刚刚注意到您有i修饰符。如果您想重新启用它,只需添加它并取出A-ZA-Z不会对i造成任何伤害,只是多余的)。

目前不需要preg_quote,但是如果您将来的术语中有特殊的正则表达式字符,这将是必要的;最好现在就包含它。