Jump to content

how to display identical or partially similar words to the search result


the25thelement

Recommended Posts

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();
}
?>

Link to comment
Share on other sites

"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.

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.