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 Link to comment https://forums.phpfreaks.com/topic/101595-help-needed-with-arrays-in-php/ 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 Link to comment https://forums.phpfreaks.com/topic/101595-help-needed-with-arrays-in-php/#findComment-519758 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. Link to comment https://forums.phpfreaks.com/topic/101595-help-needed-with-arrays-in-php/#findComment-519763 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 Link to comment https://forums.phpfreaks.com/topic/101595-help-needed-with-arrays-in-php/#findComment-519771 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; } Link to comment https://forums.phpfreaks.com/topic/101595-help-needed-with-arrays-in-php/#findComment-519778 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. Link to comment https://forums.phpfreaks.com/topic/101595-help-needed-with-arrays-in-php/#findComment-519789 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 Link to comment https://forums.phpfreaks.com/topic/101595-help-needed-with-arrays-in-php/#findComment-519799 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! Link to comment https://forums.phpfreaks.com/topic/101595-help-needed-with-arrays-in-php/#findComment-519807 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. Link to comment https://forums.phpfreaks.com/topic/101595-help-needed-with-arrays-in-php/#findComment-519818 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 Link to comment https://forums.phpfreaks.com/topic/101595-help-needed-with-arrays-in-php/#findComment-519853 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; ?> Link to comment https://forums.phpfreaks.com/topic/101595-help-needed-with-arrays-in-php/#findComment-519863 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.