-6

如何匹配单词橄榄球和足球?在正则表达式的帮助下同义词:美式足球、协会足球、加拿大足球、格子游戏、烤架消遣、橄榄球、足球、猪皮运动

4

1 回答 1

2

不要为此使用正则表达式。只需使用一个简单的数组并遍历它并检查strpos

$string = 'My friends played soccer all winter';

$words = array('rugby', 'soccer', 'American football', 'Association football', 'Canadian football', 'grid game', 'gridiron pasttime', 'rugby', 'soccer', 'the pigskin sport');

$matchFound = false;
foreach ($words as $word) {
    if (strpos($string, $word) !== false) {
        $matchFound = true;
        break;
    }
}

var_dump($matchFound);
于 2013-06-09T04:38:05.323 回答