我正在使用W3C Live Ajax Search here。他们的代码只搜索一个元素,即“标题”。我想让用户的查询和搜索多个元素,例如'title'和'url'。
<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("live.xml");
$x=$xmlDoc->getElementsByTagName('link');
//get the q parameter from URL
$q=$_GET["q"];
//lookup all links from the xml file if length of q>0
if (strlen($q)>0)
{
$hint="";
for($i=0; $i<($x->length); $i++)
{
$d=$x->item($i)->getElementsByTagName('title');
$z=$x->item($i)->getElementsByTagName('url');
if ($d->item(0)->nodeType==1)
{
//find a link matching the search text
if (stristr($d->item(0)->childNodes->item(0)->nodeValue,$q) !=false)
{
if ($hint=="")
{
$hint=
$z->item(0)->childNodes->item(0)->nodeValue . "<br />" .
$d->item(0)->childNodes->item(0)->nodeValue;
}
else
{
$hint=$hint . "<br /><br />" .
$z->item(0)->childNodes->item(0)->nodeValue . "<br />" .
$d->item(0)->childNodes->item(0)->nodeValue;
}
}
}
}
}
// Set output to "no suggestion" if no hint were found
// or to the correct values
if ($hint=="")
{
$response="no suggestion";
}
else
{
$response=$hint;
}
//output the response
echo $response;
?>
有人能举例说明如何使其发挥作用吗?根据我的理解,stristr只搜索一个haystack而你不能使用数组,那么还有另外一种方法吗?提前谢谢!