Make search in text file case-insensitive

孤街醉人 提交于 2021-02-20 04:08:01

问题


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

回答1:


Add i in addition to the m after the last / in your pattern string:

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


来源:https://stackoverflow.com/questions/27339034/make-search-in-text-file-case-insensitive

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!