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 (...) {

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.

  • Like 1
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.