使搜索文本文件不区分大小写

时间:2014-12-07 02:47:44

标签: php preg-match-all case-insensitive preg-quote

我使用以下功能搜索文本文件中的单词。问题是此功能区分大小写。我不希望它区分大小写!如何使函数不区分大小写?

$url = 'files/file.txt';
$thedata = file_get_contents($url);
$lines = explode(PHP_EOL, $thedata);

$search = 'some search words';
$pattern = preg_quote($search, '/');
$pattern = "/^.*$pattern.*\$/m";

if(preg_match_all($pattern, $thedata, $matches)) {
    echo implode('<br>', $matches[0]);    
} else {
    echo '<div class="message color-red">';
        echo 'Didn\'t find anything on that search!';
    echo '</div>';
}

1 个答案:

答案 0 :(得分:2)

除了模式字符串中最后i之后的m之外,添加/

$pattern = "/^.*$pattern.*\$/mi"