简体   繁体   English

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

[英]Make search in text file case-insensitive

I'm using the following function to search for words in a text file. 我正在使用以下功能在文本文件中搜索单词。 The problem is that this function is case-sensitive. 问题在于此功能区分大小写。 I don't want it to be case-sensitive! 我不希望它区分大小写! How can I make the function case-insensitive? 如何使函数不区分大小写?

$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>';
}

在模式字符串的最后一个/后面的m后面加上i

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM