【发布时间】:2015-01-23 03:28:41
【问题描述】:
我正在开发一个系统来搜索用户在 php 文件中键入的单词,使用没有 MySQL 的 PHP,但我遇到了问题。当文件中没有换行符时,系统运行良好。例如,如果我在包含文本“早上好”的文件中搜索“好”这个词可以正常工作,但如果我在包含文本“好
morning”的文件中搜索“好”(使用换行符)它不会因此列出文件。这是我的代码:
index.php
<form action="busca.php" method="get">
<input type="text" name="s"><br>
<input type="submit">
</form>
busca.php
<?php
$pesq = (isset($_GET['s'])) ? trim($_GET['s']) : '';
if (empty($pesq)) {
echo 'Type something.';
} else {
$index = "index.php";
$busca = glob("posts/content/*.php", GLOB_BRACE);
$lendo = "";
$conteudo = "";
foreach ($busca as $item) {
if ($item !== $index) {
$abrir = fopen($item, "r");
while (!feof($abrir)) {
$lendo = fgets($abrir);
$conteudo .= $lendo;
$lendo .= strip_tags($lendo);
}
if (stristr($lendo, $pesq) == true) {
$dados = str_replace(".php", "", $item);
$dados = basename($dados);
$result[] = "<a href=\"posts/$dados.php\">$dados</a>";
unset($dados);
}
fclose($abrir);
}
}
if (isset($result) && count($result) > 0) {
$result = array_unique($result);
echo '<ul>';
foreach ($result as $link) {
echo "<li>$link</li>";
}
echo '</ul>';
} else {
echo 'No results';
}
}
?>
【问题讨论】:
-
这是错误的:
$lendo .= strip_tags($lendo);。应该是$lendo = strip_tags($lendo);。当您可以使用file_get_contents()轻松将文件加载到变量中时,为什么还要为循环而烦恼? -
我应该在哪里使用 file_get_contents()?我猜循环是搜索所必需的
-
请看我的回答