gazfocus Posted April 17, 2008 Share Posted April 17, 2008 I am trying to add a text box with a spell checker to my website, and I have got an array called $dictionary with the words in, and then I have a checkbox (which is $_POST[text]). The code I have so far is: if ($_POST[spell] == 'yes') { $check = explode(" ",$_POST[text]); foreach ($dictionary as $currentword) { foreach ($check as $usercurrentword) { $notfound = true; if ($usercurrentword == $currentword) { $notfound = false; $output = $output . $output; } else { $output = $output . "<font color=\"blue\"><ul>" . $usercurrentword . "</ul></font>"; } } } echo $output; } But this just produces several copies of the output (corresponding with the amonut of words in the dictionary). Can anyone advise where I am going wrong? Thanks Quote Link to comment Share on other sites More sharing options...
discomatt Posted April 17, 2008 Share Posted April 17, 2008 Simplify <?php if ($_POST[spell] == 'yes') { $words = explode(" ",$_POST[text]); foreach ($words as $word) { if ( empty($word) ) // Skip empty values continue; if ( !in_array($word, $dictionary) ) $errors[] = $word; } print_r($errors); } ?> As an example Quote Link to comment Share on other sites More sharing options...
Zhadus Posted April 17, 2008 Share Posted April 17, 2008 Otherwise this should fix what you have. if ($_POST[spell] == 'yes') { $check = explode(" ",$_POST[text]); foreach ($check as $usercurrentword) { foreach ($dictionary as $usercurrentword) { $notfound = true; if ($usercurrentword == $currentword) { $notfound = false; $output = $output . $output; } else { $output = $output . "<font color=\"blue\"><ul>" . $usercurrentword . "</ul></font>"; } } } echo $output; } I took the echoed output out of the loop so you won't get it a ton of times and reversed the foreach() loops you had so that the main listing is for the text itself and the 2nd one is checking each word in the dictionary against it. Quote Link to comment Share on other sites More sharing options...
gazfocus Posted April 17, 2008 Author Share Posted April 17, 2008 Otherwise this should fix what you have. if ($_POST[spell] == 'yes') { $check = explode(" ",$_POST[text]); foreach ($check as $usercurrentword) { foreach ($dictionary as $usercurrentword) { $notfound = true; if ($usercurrentword == $currentword) { $notfound = false; $output = $output . $output; } else { $output = $output . "<font color=\"blue\"><ul>" . $usercurrentword . "</ul></font>"; } } } echo $output; } I took the echoed output out of the loop so you won't get it a ton of times and reversed the foreach() loops you had so that the main listing is for the text itself and the 2nd one is checking each word in the dictionary against it. I just tried this code and it just echos each word in the dictionary array twice. Any ideas? Thanks Quote Link to comment Share on other sites More sharing options...
Zhadus Posted April 17, 2008 Share Posted April 17, 2008 Sorry I forgot to change the key for the dictionary. Additionally I cleaned it up a little bit, as it was, if the words matched you wouldn't have displayed the word at all. Try this here: if ($_POST[spell] == 'yes') { $check = explode(" ",$_POST[text]); foreach ($check as $usercurrentword) { foreach ($dictionary as $currentword) { $notfound = true; if ($usercurrentword == $currentword) { $notfound = false; $output .= " " . $usercurrentword; } else { $output .= " <font color=\"blue\"><ul>" . $usercurrentword . "</ul></font>"; } } } echo $output; } Quote Link to comment Share on other sites More sharing options...
gazfocus Posted April 17, 2008 Author Share Posted April 17, 2008 Sorry I forgot to change the key for the dictionary. Additionally I cleaned it up a little bit, as it was, if the words matched you wouldn't have displayed the word at all. Try this here: if ($_POST[spell] == 'yes') { $check = explode(" ",$_POST[text]); foreach ($check as $usercurrentword) { foreach ($dictionary as $currentword) { $notfound = true; if ($usercurrentword == $currentword) { $notfound = false; $output .= " " . $usercurrentword; } else { $output .= " <font color=\"blue\"><ul>" . $usercurrentword . "</ul></font>"; } } } echo $output; } Thanks for your help I really appreciate it. When I try that code, I get the the the the the the the the the the the the the the the the james james james james james james james james james james james james james james james james james the word 'the' is in the dictionary, and 'james' isn't. Quote Link to comment Share on other sites More sharing options...
Zhadus Posted April 17, 2008 Share Posted April 17, 2008 if ($_POST[spell] == 'yes') { $check = explode(" ", $_POST[text]); foreach ($check as $usercurrentword) { $notfound = true; foreach ($dictionary as $currentword) { if ($usercurrentword == $currentword) $notfound = false; } if ($notfound == false) { $output .= " " . $usercurrentword; } else { $output .= " <font color=\"blue\"><b>" . $usercurrentword . "</b></font>"; } } echo $output; } Tested this time! And it works! I replaced the underline script with bold because it was screwing up the format on my browser O.o anyway, I set up a custom message and a short dictionary and it turned out ok for me. Hopefully this will be the last update Quote Link to comment Share on other sites More sharing options...
gazfocus Posted April 17, 2008 Author Share Posted April 17, 2008 if ($_POST[spell] == 'yes') { $check = explode(" ", $_POST[text]); foreach ($check as $usercurrentword) { $notfound = true; foreach ($dictionary as $currentword) { if ($usercurrentword == $currentword) $notfound = false; } if ($notfound == false) { $output .= " " . $usercurrentword; } else { $output .= " <font color=\"blue\"><b>" . $usercurrentword . "</b></font>"; } } echo $output; } Tested this time! And it works! I replaced the underline script with bold because it was screwing up the format on my browser O.o anyway, I set up a custom message and a short dictionary and it turned out ok for me. Hopefully this will be the last update Whooooooooo it worked...many thanks. You're a star! Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 17, 2008 Share Posted April 17, 2008 As it stands, you'll run into issues with capitalization. You can overcome this by changing this line: if ($usercurrentword == $currentword) To: if (strtolower($usercurrentword) == $currentword) And making sure every word in your dictionary is in lower case. Though you will still have issues with punctuation. Quote Link to comment Share on other sites More sharing options...
gazfocus Posted April 17, 2008 Author Share Posted April 17, 2008 As it stands, you'll run into issues with capitalization. You can overcome this by changing this line: if ($usercurrentword == $currentword) To: if (strtolower($usercurrentword) == $currentword) And making sure every word in your dictionary is in lower case. Though you will still have issues with punctuation. Thanks. That's helpful. Is there a way around the punctuation issue? (maybe is there a way to mass delete any punctuation during the explode?) Thanks again Quote Link to comment Share on other sites More sharing options...
GingerRobot Posted April 17, 2008 Share Posted April 17, 2008 You'd have to go into regular expressions. Im sure it would be much more efficient to do this with the split, otherwise you'd have the regex overhead on every single comparison. I don't think that could be easily incorporated into the above. So try: <?php $dictionary = array('this','is','a','test','dictionary'); $sentence = 'This? is a "test" sentence'; $words = preg_split('/(\.|\?|!| |,|;|:|&|\'|")/',$sentence,-1,PREG_SPLIT_NO_EMPTY);//if you need to add other punctuation, separate it with a pipe(|). If its a special character, you'll need to stick a backslash before it. foreach($words as $k => $v){ $words[$k] = strtolower($v); } $errors = array_diff($words,$dictionary); foreach($errors as $v){ $sentence = str_ireplace( $v ,'<font color="blue"><b>'.$v.'</b></font>',$sentence); } echo $sentence; ?> 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.