正则表达式.简单的任务


Regular expression. Simple task

如何匹配橄榄球和足球?借助regexp同义词:美式足球,英式足球,加拿大足球,格子游戏,英式橄榄球,橄榄球,足球,猪皮运动

不要使用正则表达式。只需使用一个简单的数组并遍历它并检查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);