0

この w3school の例を使用して作成した ajax ライブ検索バーがあります。http://www.w3schools.com/php/php_ajax_livesearch.asp

単語内の文字のチャンクに一致するのではなく、最初の文字から始まる同じ順序の文字を持つ結果のみを表示するにはどうすればよいですか。たとえば、結果として「COMPUTER」を表示するには、「PUTER」ではなく「COM...」と入力する必要があります。

私はそれをここのどこかにあることに絞り込んだと信じています:

//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++)
  {
  $y=$x->item($i)->getElementsByTagName('title');
  $z=$x->item($i)->getElementsByTagName('url');
  if ($y->item(0)->nodeType==1)
    {
    //find a link matching the search text
    if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q))
      {
      if ($hint=="")
        {
        $hint="<a href='" . 
        $z->item(0)->childNodes->item(0)->nodeValue . 
        "' target='_blank'>" . 
        $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
        }
      else
        {
        $hint=$hint . "<a href='" . 
        $z->item(0)->childNodes->item(0)->nodeValue . 
        "' target='_blank'>" . 
        $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
        }
      }
    }
  }
}
4

1 に答える 1