我从w3schools找到了这个代码(不要评判我)。
aspphp.asp
这是一个代码,允许您创建一个类似于Google使用AJAX的搜索字段。
代码工作正常,但是在我的.php中字符串超过一个字,只有在搜索第一个单词时,它才能正确地输出。如果我试图在输入字段中放置字符串中的任何内容,它就不会输出。例如,我的字符串可能是:
$a[]="This is string 1."
$a[]="This is string 2."
$a[]="Is this string 3?"
$a[]="Is this string 4?"问题是,如果我输入“This”,它只会显示从这个开始的结果。如果我放“string”,它会说没有结果。有什么想法吗?我确信问题在PHP部分中:
$q=$_REQUEST["q"]; $hint="";
if ($q !== "")
{ $q=strtolower($q); $len=strlen($q);
foreach($a as $name)
{ if (stristr($q, substr($name,0,$len)))
{ if ($hint==="")
{ $hint=$name; }
else
{ $hint .= "<br>$name"; }
}
}
}
// Output "no suggestion" if no hint were found
// or output the correct values
echo $hint==="" ? "Refine search values" : $hint;
?>发布于 2014-07-21 00:10:24
这个问题正在发生,因为这一行。
substr($name, 0, $len)只匹配字符串开头的a[]内容。
你的代码应该是
if ($q !== "")
{ $q=strtolower($q); // not needed //$len=strlen($q);
foreach($a as $name)
{ if (stristr($name, $q))
{ if ($hint==="")
{ $hint=$name; }
else
{ $hint .= "<br>$name"; }
}
}
}https://stackoverflow.com/questions/24856055
复制相似问题