2014-12-07 84 views
0

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

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

'/^.*$模式* \ $/im'? – sjagr 2014-12-07 02:49:00

+0

有'i'修饰符 – zerkms 2014-12-07 02:49:19

+0

[Regex区分大小写]可能重复(http://stackoverflow.com/questions/9655164/regex-case-sensitive) – sjagr 2014-12-07 02:50:12

回答

2

除了m在你的模式串最后/后添加i

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

非常感谢! :D我会尽我所能接受你的答案 – Erik 2014-12-07 02:50:58

相关问题