Padgoi Posted October 5, 2007 Share Posted October 5, 2007 Hi, ok, so I just installed this new script on my site that checks spelling for each word posted and it breaks down tags as well to check spelling. The problem is that because it's breaking down the tags, it's not actually posting the tags, it's completely deleting them. Here's the part of the script causing the issue: /* get rid of all the words that are inside HTML tags, while still keeping each word's offset */ preg_match_all("/[<>]/", $row['post'], $matched, PREG_OFFSET_CAPTURE); $matched = $matched[0]; $opened = array(); foreach($matched as $tag){ if($tag[0] == "<"){ $opened[] = $tag[1]; } else { if(count($opened) > 0){ foreach ($words as $off => $word){ if($off > $opened[count($opened)-1] && $off < $tag[1]){ unset($words[$off]); } if($word == "" || $word == " "){ unset($words[$off]); } } array_pop($opened); } } } /* Now we are left with an array, $words which contains each word and its offset i.e. $words[offset] = word */ /* Spell check each word! */ $i = 0; foreach($words as $off => $word){ $check = SmartnessCheckWord($word, $row['pid']); if(!$check){ //i.e. if it returns false (word is WRONG) $row['points'] = $row['points'] - 1; // replace word $words[$off] = "<sp>".$word."</sp>"; $row['post'] = substr_replace($row['post'], $words[$off], $off+(9*$i), strlen($word)); $i++; } } This is breaking down all HTML tags in order to check spelling, but I want to check the spelling inside the tags WHILE keeping the tags intact so they actually work. Sorry, it's kinda difficult to explain, but if anyone could help, I'd be really appreciative. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/71991-php-breaking-down-tags-to-check-spelling/ Share on other sites More sharing options...
kathas Posted October 5, 2007 Share Posted October 5, 2007 You could keep the post in another variable and then spellcheck this one... If it finds an error replace it in the other variable... Then use the other variable that is spellchecked and has all the html tags untouched Example.: <?php /* get rid of all the words that are inside HTML tags, while still keeping each word's offset */ $post = $row['post']; // !!Check that line !! // preg_match_all("/[<>]/", $row['post'], $matched, PREG_OFFSET_CAPTURE); $matched = $matched[0]; $opened = array(); foreach($matched as $tag){ if($tag[0] == "<"){ $opened[] = $tag[1]; } else { if(count($opened) > 0){ foreach ($words as $off => $word){ if($off > $opened[count($opened)-1] && $off < $tag[1]){ unset($words[$off]); } if($word == "" || $word == " "){ unset($words[$off]); } } array_pop($opened); } } } /* Now we are left with an array, $words which contains each word and its offset i.e. $words[offset] = word */ /* Spell check each word! */ $i = 0; foreach($words as $off => $word){ $check = SmartnessCheckWord($word, $row['pid']); if(!$check){ //i.e. if it returns false (word is WRONG) $row['points'] = $row['points'] - 1; // replace word $words[$off] = "<sp>".$word."</sp>"; //since you have the wrong word you don't need to replace it // using substr_replace but str_replace instead // so use sth like $post = str_replace($word,"whatever you replace it with",$post); // $post is the assigned var see top //$row['post'] = substr_replace($row['post'], $words[$off], $off+(9*$i), strlen($word)); $i++; } } Quote Link to comment https://forums.phpfreaks.com/topic/71991-php-breaking-down-tags-to-check-spelling/#findComment-362662 Share on other sites More sharing options...
Padgoi Posted October 5, 2007 Author Share Posted October 5, 2007 Thanks, but that didn't solve the issue, when I refresh the page (and it goes through the spellcheck process), it's still breaking down the Tag and only showing the words INSIDE of the tag, but not the actual tag itself. For instance, if I quote someone, instead of showing the quote box, it will only show the quote itself, but not the image that is called by the tag to separate it from the rest of the post as being a quote, if that makes any sense. Quote Link to comment https://forums.phpfreaks.com/topic/71991-php-breaking-down-tags-to-check-spelling/#findComment-362681 Share on other sites More sharing options...
kathas Posted October 5, 2007 Share Posted October 5, 2007 I understood the problem before but i cant understand why it still does that... OK let's take the problem from the beginning... <?php $post = $row['post']; preg_match_all("/<(.*)>/", $post, $matches); $matches = $matches[0]; foreach ($matches as $tag) { str_replace($tag,"",$row['post']); } // now $row['post'] is tag free // i see you want each word in an array element so $words = explode(" ",$row['post']); // Let's spell check foreach ($words as $word) { $check = SmartnessCheckWord($word, $row['pid']); if(!$check){ //i.e. if it returns false (word is WRONG) $row['points'] = $row['points'] - 1; // replace word $word_rep = "<sp>".$word."</sp>"; // now replace the words str_replace($word,$word_rep,$post); } // $post is spell checked I havent tested the above code but the logic is correct... Check the regex syntax and everything... It must be right though Quote Link to comment https://forums.phpfreaks.com/topic/71991-php-breaking-down-tags-to-check-spelling/#findComment-362712 Share on other sites More sharing options...
Padgoi Posted October 5, 2007 Author Share Posted October 5, 2007 Wait, my php skills are not advanced, that code you just posted, is that the entire code I should replace the old code with to see if it works? Quote Link to comment https://forums.phpfreaks.com/topic/71991-php-breaking-down-tags-to-check-spelling/#findComment-362737 Share on other sites More sharing options...
Padgoi Posted October 5, 2007 Author Share Posted October 5, 2007 Yeah, that didn't work either, it might be something else in the script, I'll have to contact the original creator. If you can, you can contact me via AIM (Padgoi) or MSN (Padgoi1@hotmail.com). Thanks for the help though. Quote Link to comment https://forums.phpfreaks.com/topic/71991-php-breaking-down-tags-to-check-spelling/#findComment-362753 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.