简体   繁体   中英

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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