Pregunta

I'm working on a custom spell checker (and learning as I go) that needs to do the following upon form submission:

  1. Convert the string submitted from textarea into an array of words.
  2. Search for each word in the array in the mysql english table.
  3. If the word does not exist, mark it as misspelled by wrapping it in an HTML tag.
  4. Convert the array back to a string including original punctuation, line breaks and the newly-tagged words.

I've searched many a post as well as php.net and I've come up with the following code that does everything except #4 and I've been stuck there for a couple days (I assume I need to add something in or before #1 for the preservation part but I can't seem to wrap my head around it):

Current code:

$inputContents = $_POST['compose'];
$inputContents = preg_replace("/[^a-zA-Z ']+/", ' ', $inputContents);
$inputContents = trim($inputContents);
$inputContents = preg_split("/\s+/", $inputContents);

var_dump($inputContents);

foreach ($inputContents as $singleWord) {
  $word_exists = mysql_query("SELECT `word` FROM `english` WHERE `word` = '". mysql_real_escape_string($singleWord) ."'") or die(mysql_error());
  $word_exists = mysql_num_rows($word_exists);
  if ($word_exists !== 0) {
    echo $singleWord ." ";
  }else{
    echo "<span id='misspelledWord' style='font-style:italic;'>". $singleWord ."</span>&nbsp;";
  }
}

<form action="spellcheck_test.php" method="POST">
  <textarea name="compose"></textarea>
  <input type="submit" value="Post!" />
</form>

Using the code above, if my string is:

Spellling games; they're alot of fun!

Then my array becomes:

array(6) { [0]=> string(9) "Spellling" [1]=> string(5) "games" [2]=> string(7) "they're" [3]=> string(4) "alot" [4]=> string(2) "of" [5]=> string(3) "fun" }

And the code outputs:

Spellling games they're alot of fun

But I'd like it to output:

Spellling games; they're alot of fun!

Any suggestions on accomplishing #4?

¿Fue útil?

Solución

For 1. you should use explode(' ',$inputContents) and you'll endup with:

{ [0]=> "Spellling" [1]=> "games;" [2]=> "they're" [3]=> "alot" [4]=> "of" [5]=> "fun!" }

Then create a new array $checkedWords=array(); and take care of 2.

foreach ($inputContents as $singleWord) {
  //your MySQL checks here;
  //mysql_ functions were deprecated, use mysqli or PDO instead
  preg_match('/([a-zA-Z\']*)(\W*)/',$singleWord,$parts);
  //check parts[0] against MySQL here
  //$parts[1] is your word without other following chars
  //$parts[2] are any characters following your word
  if (isset($parts[2]) $parts[2]=''; 
  if ($word_exists !== 0) {
     $checkedWords[]=$parts[1].$parts[2];
  }else{
     $checkedWords[]="<span class='misspelledWord'>".$parts[1]."</span>".$parts[2];
  }
}

To get to 4. you'll just have to implode(' ',$checkedWords)

Otros consejos

Here's the final code I ended up with. Made some slight changes during testing to fix problems.

if (isset($_POST['compose'])) {
  $inputContents = $_POST['compose'];
  $paragraph =     preg_split("/\n+/m", $inputContents);

  foreach ($paragraph as $singleWords) {
    $singleWords =   preg_split("/\s+/m", $singleWords);
    $combineWords =  array();

    foreach ($singleWords as $singleWord) {
      preg_match('/(["]*)([a-zA-Z\'-]*)([\W0-9]*)/',$singleWord,$parts);
      $word_exists =  mysql_query("SELECT `word` FROM `english` WHERE `word` = '". mysql_real_escape_string($parts[2]) ."'") or die(mysql_error());
      $word_exists =  mysql_num_rows($word_exists);
      $checkedWords = array();

      if ($word_exists !== 0) {
        $checkedWords[$singleWord] = $parts[1].$parts[2].$parts[3];
      }else{
        $checkedWords[$singleWord] = $parts[1]."<span class='misspelledWord'>".$parts[2]."</span>".$parts[3];
      }
    $combineWords[] = implode(' ',$checkedWords);
    }
    $reformParagraph[] = implode(' ',$combineWords);
  }
  $reformContents = implode("<br />",$reformParagraph);
  echo "<p>".$reformContents."</p>";
}

And the form...

<form action="spellcheck_test.php" method="POST">
  <textarea name="compose"></textarea>
  <input type="submit" name="submitPart" value="Post!" />
</form>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top