在php中搜索2个单词

在php中搜索2个单词,php,Php,所以我有一个php页面,在这里你可以输入一个单词,如果它在一个标题或描述中,它会显示标题和描述。现在我明白了: $title='hoooi'; $description="Lorem Ipsum is simply dummy text of the printing and typesetting industry."; if (isset($_GET['zoek'])){ $zoekwoord=$_GET['zoek']; if($alles_goed==true){ $z

所以我有一个php页面,在这里你可以输入一个单词,如果它在一个标题或描述中,它会显示标题和描述。现在我明白了:

$title='hoooi';
$description="Lorem Ipsum is simply dummy text of the printing and typesetting industry.";
if (isset($_GET['zoek'])){
    $zoekwoord=$_GET['zoek'];

if($alles_goed==true){
    $zoekwoordreal= explode(" ", $zoekwoord);
    foreach($zoekwoordreal as $word){
        $zoek_title_en_description=$title . $description;
        if($zoekwoord==""){
        }else{
            $pos=stripos($zoek_title_en_description,$word);
        }
        if($pos!==false){
        echo $title . $description;
                    }
        }
        }
        }
    echo <<<EOT
<table>
<form action="zoek.php" method="get">
    <tr><th>Zoek: </th><td><input type="text" name="zoek" value=""></td></tr>
    <tr><th><input type="submit" value="submit"></td></tr>
</form> 
</table
EOT;
$title='hooi';
$description=“Lorem Ipsum只是印刷和排版行业的虚拟文本。”;
如果(isset($_GET['zoek'])){
$zoekwoord=$_GET['zoek'];
如果($alles_goed==true){
$zoekwoordreal=explode(“,$zoekwoord”);
foreach($zoekwoordreal作为$word){
$zoek_title_en_description=$title.$description;
如果($zoekwoord==“”){
}否则{
$pos=stripos($zoek_title_en_description,$word);
}
如果($pos!==false){
echo$title.$description;
}
}
}
}

echo您可以使用
explode()
preg_split('/\s+/',…)
将查询字符串拆分为单词,然后使用
foreach()
循环并检查每个单词。

记住,要添加第二个名为“zoek2”的关键字输入框

对于要搜索多个关键字并继续使用单个输入框的情况: PS:所有单词都将被
空格拆分,例如:“karl是伟大的”,总单词数:3

if (isset($_GET['zoek']) && !empty($_GET['zoek']) ){
    $zoekwoords = explode(" ", $_GET['zoek']);
    $foundAll = true;

    if($alles_goed==true){
        $zoek_title_en_description=$title . $description;

        foreach( $zoekwoords as $zoekwoord ){
            $pos=stripos($zoek_title_en_description,$zoekwoord);
            if($pos===false){
                $foundAll=false;
                break; //add break to speed up
            }
        }
        if($foundAll !== false){
            echo $title . $description;
        }
    }
}

现在是你来尝试实现它的时候了。我已经说过“如何”了,但你现在必须这样做。如果你不能让它工作,请带着你的新代码回来,我会帮助你。现在你必须用
foreach($zoekwoordreal as$word)循环你的单词数组
$zoekwoordreal
{/*当我搜索两个单词时,请在这里检查你的stripos*/}
,它两次显示标题和描述。当我搜索3个单词时,我会得到3次。stripos会在字符串中找到子字符串的位置。因此,它会找到“Lorem ipsum”或“ipsum is”,但不会找到“Lorem is”,因为它不以字符串形式出现。我认为您希望在描述中搜索两个单独的字符串(单词)。这不是一个好的解决方案吗?如果我想搜索5个单词呢?我必须有5个文本框?那不可能。嗯。。。。我认为你应该问“多个关键字”,而不是你的问题中的“2”!对于多个关键字:阅读下一个关键字的“我的代码”。如果此If
If($pos==false)
为真,您也可以
中断原因如果没有找到一个关键字,那么它就结束了,您不必检查剩下的所有关键字。如果(设置($\u GET['zoek')&&!空($\u GET['zoek'))您缺少一个(?@Loko没有缺少任何
@TiMESPLiNTER,谢谢您的提醒
if (isset($_GET['zoek']) && !empty($_GET['zoek']) ){
    $zoekwoords = explode(" ", $_GET['zoek']);
    $foundAll = true;

    if($alles_goed==true){
        $zoek_title_en_description=$title . $description;

        foreach( $zoekwoords as $zoekwoord ){
            $pos=stripos($zoek_title_en_description,$zoekwoord);
            if($pos===false){
                $foundAll=false;
                break; //add break to speed up
            }
        }
        if($foundAll !== false){
            echo $title . $description;
        }
    }
}