Jump to content

How to show the suggestion text according to prefix


salimalisalim

Recommended Posts

Hi,
I have the below code to show the suggestion text, now it’s showing all the words which contain the letter when I first type in the text field. For example, If I typed the letter “a”, it should show only the words which start with the letter “a”, now it’s showing all the words which contain the letter “a” in the suggestion. Below is my code. Thanks in advance.

 


<?php
$xmlDoc=new DOMDocument();
$xmlDoc->load("links.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++) {
    $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 . "<br /><a href='" .
          $z->item(0)->childNodes->item(0)->nodeValue .
          "' target='_blank'>" .
          $y->item(0)->childNodes->item(0)->nodeValue . "</a>";
        }
      }
    }
  }
}

// Set output to "no suggestion" if no hint was found
// or to the correct values
if ($hint=="") {
  $response="no suggestion";
} else {
  $response=$hint;
}

//output the response
echo $response;
?> 

 

Link to comment
Share on other sites

if (stristr($y->item(0)->childNodes->item(0)->nodeValue,$q)) {

That is the bit to check if the string (the first argument) contains the desired letter/string (second argument). That's what you need to change. I suggest strncasecmp; the length to check should be the length of $q, not the ->nodeValue.

Link to comment
Share on other sites

if (...) {

makes the decision about whether to include the item from the XML in the search results. The part inside it needs to be true.

stristr(..., ...)

What the stristr function is effectively doing is checking if the first argument (a string) contains the second argument (also a string). It doesn't technically return true (or false) like the if expects, but it's close enough that PHP allows it.

What you want is not "contains" but "starts with". Unfortunately PHP doesn't have a "starts with" function, but the strncasecmp I linked comes close: it takes two strings and compares the first few characters (you decide how many) to see if they match. The "n" means it compares a length you want, and "case" meaning it's case-insensitive (like the "i" in "stristr" means it's case-insensitive... yeah, one is "case" and one is "i", that's just the way it is). The function returns a number that measures how the two compare, and that number is 0 if they are the same.

Since you want "starts with", the length you need to compare is the length of the string you are searching for. "If the first (length of $q) characters are the same...". strlen is the function to get the length.

So you need to:
- Keep the overall if(...) structure
- Use strncasecmp instead of stristr, with the first two arguments being the two strings to compare (it doesn't actually matter which is which) and the third being the length to compare
- Use strlen with $q for the length
- Check that the strncasecmp function returns a number == 0

Give that a shot, and if you have problems then post what you came up with and a description of what's not working.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.