2

私は、フォームの送信時に次のことを行う必要があるカスタムスペルチェッカーに取り組んでいます(そして、学習しながら学習しています):

  1. textarea から送信された文字列を単語の配列に変換します。
  2. englishmysqlテーブルの配列内の各単語を検索します。
  3. 単語が存在しない場合は、HTML タグでラップしてスペルミスとしてマークします。
  4. 配列を変換して、元の句読点、改行、および新しくタグ付けされた単語を含む文字列に戻します。

私はphp.netだけでなく多くの投稿を検索し、#4を除くすべてを実行する次のコードを思いつきました.2、3日間そこに行き詰まっています(または保存部分の#1の前ですが、頭を包むことができないようです):

現在のコード:

$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>

私の文字列が次の場合、上記のコードを使用します。

Spellling games; they're alot of fun!

次に、私の配列は次のようになります。

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" }

コードは次のように出力します。

スペリングゲームはとても楽しいです

しかし、私はそれを出力したい:

スペリングゲーム; 彼らはとても楽しいです!

#4を達成するための提案はありますか?

4

2 に答える 2

0

これが私が最終的に得た最終的なコードです。問題を修正するために、テスト中に若干の変更を加えました。

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

そして形...

<form action="spellcheck_test.php" method="POST">
  <textarea name="compose"></textarea>
  <input type="submit" name="submitPart" value="Post!" />
</form>
于 2013-01-07T03:55:34.757 に答える
0

1.の場合、使用する必要があり、最終的には次のようexplode(' ',$inputContents)になります。

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

次に、新しい配列$checkedWords=array();を作成し、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];
  }
}

4.に到達するには、次のことを行う必要がありますimplode(' ',$checkedWords)

于 2013-01-05T22:59:52.570 に答える