Jump to content

Php - breaking down tags to check spelling...


Padgoi

Recommended Posts

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.

Link to comment
Share on other sites

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++;
  			}
  		}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

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.