如何在搜索txt文档时忽略大小写


How to disregard case when searching a txt document?

我的网站上有一个搜索框,它从网站列表(存储在sites.txt中)中进行搜索,列表如下所示:

<a href="example.org">Example Website</a>
<a href="example.com">Another Example Website</a>

搜索框使用此操作从列表中查找匹配的结果:

<?php
    $q = $_REQUEST["q"];
$f = fopen("sites.txt", "r");
while (($line = fgets($f)) !== FALSE) {
   if (strstr($line, $q)) {
       print "<p>$line</p>";
   }
}
?>

如何更改PHP以确保忽略搜索框输入的大小写?

I.E:搜索"其他"返回"另一个示例网站"

if (strstr($line, $q)) {

为了使其不区分大小写,只需将strstr()更改为使用stristr()(带有i)函数即可。

参考:
http://php.net/manual/en/function.stristr.php