我正在局域网内构建一个小型搜索引擎。因为只有大约25个页面,我使用的是bash脚本,它会获取URL列表并将其格式化为HTML,例如:
<div id="result"><p><a href="#">Title 1</a></p><p>Description 1</p></div>
<div id="result"><p><a href="#">Title 2</a></p><p>Description 2</p></div>
然后是一个PHP脚本,它从上面的每一行中搜索GET输入,并返回每个匹配的行。我的问题是,如何使此搜索大小写不敏感?我的PHP代码如下:
<?php
// index.php
include('inc/header.html');
$searchfor = $_GET['q'];
$file = 'inc/urls.txt';
$contents = file_get_contents($file);
$pattern = preg_quote($searchfor, '/');
$pattern = "/^.*$pattern.*'$/m";
if(preg_match_all($pattern, $contents, $matches)){
echo "<p>Found matches:</p>";
echo implode("<br />", $matches[0]);
}
else{
echo "<p>No matches found.</p>";
fclose ($file);
}
include('inc/footer.html');
?>
我知道这可能很明显,我正在preg_quote
附近寻找一个/I,但我是PHP的新手,似乎找不到答案,也找不到关于SE/SO/etc 的任何类似问题
您尝试过这个$pattern = "/^.*$pattern.*'$/mi";
吗?