如何让这个搜索引擎与 subsrt_count 一起工作?

How to make this search engine work with subsrt_count?

所以,我一直致力于制作一个不使用数据库的搜索引擎。它应该做的是在网页中找到搜索的词并自动给它link。这是它的代码:

<?php

session_start();

$searchInput = $_POST['search'];

   $inputPage1 = $_SESSION['pOneText'];
   $inputPage2 = isset($_SESSION['pTwoText']) ? $_SESSION['pTwoText'] : "";
   $inputPage3 = isset($_SESSION['pThreeText']) ? $_SESSION['pThreeText'] : ""; 

    $fUrl = file_get_contents("mDummyP.php");
    $sUrl = file_get_contents("sDummyP.php");
    $tUrl = file_get_contents("tDummyP.php");

    if (substr_count($fUrl, $searchInput) !== false) {
        echo "All results for <strong> $searchInput </strong> : <br>" ;
    } elseif (substr_count($sUrl, $searchInput) !== false) {
        echo "All results for <strong> $searchInput </strong> : <br>";
    } elseif (substr_count($tUrl, $searchInput) !== false) {
        echo "All results for <strong> $searchInput </strong> : <br>";
    } else {
        echo "No resulst for <strong> $searchInput </strong>! ";
    }

    ?>

但是,它从不检查单词是否实际存在,它总是 returns "all results for"。所以,我想知道是否有人知道原因或提出改进建议。切记,绝对不会用于专业,只是为了测试自己的能力。提前致谢!

您需要查看 substr_count

的 php 手册

Return Values

This function returns an integer.

因此它总是首先进入这个 if 块:

if (substr_count($fUrl, $searchInput) !== false) {

因为 substr_count 的 return 值只是一个整数,永远不会为假 - 您的代码正在检查 false 的确切值和类型。

此外,除了 else 块之外,所有语句都只是回显完全相同的字符串,因此如果执行确实进入了 if 或 elseif 块,您将看不到任何输出差异。请参阅下文以帮助您走上正确的轨道:

$searchInput = 'some';

$fUrl = 'this is test file text';
$sUrl = 'this is some other text';
$tUrl = 'extra text';

if (substr_count($fUrl, $searchInput) !== 0) {
    echo "a";
} elseif (substr_count($sUrl, $searchInput) !== 0) {
    echo "b";
} elseif (substr_count($tUrl, $searchInput) !== 0) {
    echo "c";
} else {
    echo "No resulst for <strong> $searchInput </strong>! ";
}