在 PHP 中搜索系统的行 Brek 错误


line brek error with search system in php

我正在开发一个系统,该系统使用没有MySQL的PHP搜索用户在php文件中键入的单词,但我遇到了问题。当文件中没有换行符时,系统运行良好。例如,如果我在包含文本"早上好"的文件中搜索单词"好"可以正常工作,但是如果我在包含文本"早上好"(带换行符)的文件中搜索"好",它
不会列出该文件作为结果。这是我的代码:
索引.php

<form action="busca.php" method="get">
<input type="text" name="s"><br>
<input type="submit">
</form>

布斯卡.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';
    }
}
?>

您对stristr的用法不正确。
将其与这样的false进行比较:

if (stristr($lendo, $pesq) !== false) {

如果找到字符串 — 函数返回子字符串。哪个可以转换为布尔truefalse,你永远不知道。如果找不到它 - 它返回false - 你应该与之比较的唯一正确值。

为此

使用strpos更好。
我的变体:

foreach ($busca as $item) {
        if ($item !== $index) {
            $lendo = file_get_contents($item);
            $lendo = strip_tags($lendo);
            if (strpos($lendo, $pesq) !== false) {
                $dados    = str_replace(".php", "", basename($item));
                $result[] = "<a href='"posts/$dados.php'">$dados</a>";
            }
        }
    }

要修复换行符 - 尝试摆脱它们喜欢这个:

$lendo = file_get_contents($item);
$lendo = strip_tags($lendo);
$lendo = str_replace(["'r","'n"], ' ', $lendo);