the25thelement Posted March 30, 2011 Share Posted March 30, 2011 I am building online dictionary with php. The following code allows the user to search a word and display the definition results from the database. The problem is the user has to input the identical words only. If the words are not exactly similar to the database words it returns error message. But in this case I need it to give a suggestion along with the error message based on the few beginning words of the submitted word. eg. if the user type a word "suggestion", and the word "suggestion" is not available in the database .... instead of just the error message to also search and include other stored data closer to the submitted word ... like "TRY the word SUGGEST". can any one help me on this please. Thank you in advance. here is the code <?php $script = $_SERVER['PHP_SELF']; if(isset($_POST['sbm']) && isset($_POST['word']) ) { submited(); } else { show_form(); } function show_form() { ?> <p><center> Type <span style='font-weight:bold;'>ENGLISH</span> word Only! <span style='font-weight:bold;'>SPELL IT CORRECTLY!!!.</span></center></P> <p><center>Search</center></p> <center><form name=frm method=POST action="<?php echo $script ?>"> <p><input type=text name="word"><br /></p> <p><input type=submit name="sbm" value="Submit"> </form> </center></p> <?php } function submited() { require("dbconn.inc"); $word=$_POST['word']; $sql="select * from words where eng like '".mysql_real_escape_string($word)."'"; $result=mysql_query($sql,$link); // I assume $link is defined in dbconn.inc if(@mysql_num_rows($result)!=0) { $rows=mysql_fetch_array($result); $am=$rows["am"]; echo("<center>The Word <h1><b>$word</b></h1> in Am is : <h1><b>$am </b></h1><br></center>"); } else { echo("<center><span style='font-weight:bold;color:#FF0000;'>Could not find it! Check the spelling and try again !!</span></center>"); } // The connection will close automatically since // this is near the end of the script. mysql_close($link); show_form(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/232232-how-to-display-identical-or-partially-similar-words-to-the-search-result/ Share on other sites More sharing options...
Zane Posted March 30, 2011 Share Posted March 30, 2011 Check out levenshtein distance You could also look at soundex and metaphone. Quote Link to comment https://forums.phpfreaks.com/topic/232232-how-to-display-identical-or-partially-similar-words-to-the-search-result/#findComment-1194701 Share on other sites More sharing options...
btherl Posted March 30, 2011 Share Posted March 30, 2011 "stemming" might be the process you are looking for. A google search for "php stemming" will give you some packages that implement stemming algorithms. What Zanus has suggested might be more useful, as those can get you matches even when spelling is not correct. Quote Link to comment https://forums.phpfreaks.com/topic/232232-how-to-display-identical-or-partially-similar-words-to-the-search-result/#findComment-1194705 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.