salimalisalim Posted December 30, 2018 Share Posted December 30, 2018 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; ?> Quote Link to comment Share on other sites More sharing options...
requinix Posted December 30, 2018 Share Posted December 30, 2018 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. 1 Quote Link to comment Share on other sites More sharing options...
salimalisalim Posted December 30, 2018 Author Share Posted December 30, 2018 Thanks for the reply, So how will be my modified code ? I am new to programming so, I don't have that much idea. Quote Link to comment Share on other sites More sharing options...
requinix Posted December 30, 2018 Share Posted December 30, 2018 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. 1 Quote Link to comment Share on other sites More sharing options...
salimalisalim Posted December 30, 2018 Author Share Posted December 30, 2018 Thank you so much for the great descriptions, I modified my code as below, but still, it's giving me the previous result. if (strncasecmp ($y->item(0)->childNodes->item(0)->nodeValue,$q, 10)) { Quote Link to comment Share on other sites More sharing options...
requinix Posted December 30, 2018 Share Posted December 30, 2018 1. I don't know where you came up with 10. 2. You need to check the return value from strncasecmp. Take another look at my post. 1 Quote Link to comment Share on other sites More sharing options...
salimalisalim Posted December 30, 2018 Author Share Posted December 30, 2018 I updated like below with the length of $q, but still, I am getting all the words containing the letter if (strncasecmp ($y->item(0)->childNodes->item(0)->nodeValue,$q,strlen($q))) Quote Link to comment Share on other sites More sharing options...
requinix Posted December 30, 2018 Share Posted December 30, 2018 It's actually showing you all entries that do not begin with the letter. 3 hours ago, requinix said: - Check that the strncasecmp function returns a number == 0 Quote Link to comment Share on other sites More sharing options...
salimalisalim Posted December 31, 2018 Author Share Posted December 31, 2018 Thank you very much, It works... Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.