所以我有这个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;这是完美的工作,但只有当我键入一个词来搜索。现在我想要能够输入两个单词,如果它们匹配,显示标题和描述。此时,当我输入“Lorem is”时,它并没有给我显示标题和描述。但是当我输入"Lorem“时,它确实显示了标题和描述。这意味着当我输入两个单词时,$pos==false。我应该怎么做才能使我的php页面可以用两个单词进行搜索?
发布于 2013-11-11 09:43:38
您可以使用explode()或preg_split('/\s+/', ...)将查询字符串拆分成单词,然后用foreach()循环并检查每个单词。
发布于 2013-11-11 09:45:32
记住,要添加第二个名称为"zoek2“的关键字输入框
if (isset($_GET['zoek'])){
$zoekwoord =$_GET['zoek'];
$zoekwoord2=$_GET['zoek2'];
if($alles_goed==true){
$zoek_title_en_description=$title . $description;
if($zoekwoord!="" && $zoekwoord2!=""){
$pos=stripos($zoek_title_en_description,$zoekwoord);
$pos2=stripos($zoek_title_en_description,$zoekwoord2);
}
if($pos!==false && $pos2!==false){
echo $title . $description;
}
}
}对于这种情况,您需要搜索多个关键字,并继续使用单个输入框: PS:所有单词将被SPACE拆分,例如:"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;
}
}
}https://stackoverflow.com/questions/19903074
复制相似问题