问题
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