【问题标题】:Regular expression. Simple task [closed]正则表达式。简单的任务[关闭]
【发布时间】:2013-06-05 01:55:18
【问题描述】:

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

【问题讨论】:

  • 您的问题不清楚。你到底想达到什么目的?您只是在一段文本中寻找这些词吗?

标签: php regex


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-27
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多