2016-11-07 6 views
-1

PHPフォームに2つの入力フィールドがあります。フィールドを配列に保存する必要があります。出力には、各単語の横に括弧内の単語の長さが含まれている必要があります。どの入力フィールド1または2が最も長い単語を有し、各フィールドにいくつの単語があるかを評価する必要もある。実行中のエラーを示します。構文エラー、ファイル Screenshot 1Screenshot 2配列を文字列に分解するPHP

<!DOCTYPE html "> 
<html> 
    <head> 
     <meta charset="UTF-8"> 
      <title> Two Input field compare PHP </title> 
    </head> 
    <body> 
     <?php 

     echo "<br/>"; 

     if (isset($_POST['submit'])) { 
      $input1 = $_POST['input1']; 
      $input2 = $_POST['input2']; 
      //example $input1 = "Learning the PHP"; 
      //example $input2 = "Counting words and showing(7) like this word<< "; 

      if (empty($input1)) { 
       echo "You need to enter both fields !"; 
      } 
      if (empty($input2)) { 
       echo "You need to enter both fields !"; 
      } 

       echo $input1; 
       echo $input2; 


      $inputarr1 = explode(" ", $input1); 
      $inputarr2 = explode(" ", $input2); 



      $longestlenghtarr1 = $longestlenghtarr2 = 0; 

      $arraylength1 = sizeof($inputarr1); 
      $arraylength2 = sizeof($inputarr2); 



      $longest1 = $longest2 = 0; 

      for ($x = 0; $x < arraylength1; $x++) { 

      echo $inputarr1[$x] . " &lt; " . strlen($inputarr1[$x]) . " &gt; "; 
       if (strlen($inputarr1[$x]) > $longest1) { 
        $longest1 = strlen($inputarr1[$x]); 
       } 
      } 

      for ($y = 0; $y < arraylength2; $y++) { 
       echo $inputarr2[$y] . " &lt; " . strlen($inputarr2[$y]) . " &gt; "; 

       if (strlen($inputarr2[$y]) > $longest2) { 
        $longest2 = strlen($inputarr2[$y]); 
       } 
      } 

       if ($longest1 > $longest2) { 
       echo "<br/> The field 1 input has longest word of lenght " . $longest1." characters !"; 
       } 

       if ($longest2 > $longest1) { 
         echo "<br/> The field 2 input has longest word of lenght " .$longest2." characters !"; 
        } 

        if ($arraylength1 > $arraylength2) { 
        echo "<br/> The field 1 input has more words";} 
         if ($arraylength2 > $arraylength1) { 
          echo "<br/> The field 2 input has more words"; 
         } 

         echo "<br/>"; 

       ?> 

      <div> 
          <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post"> 

       Input Text 1 <br/> 
       <input type="text" name="input1" /><br/> 
       Input Text 2 <br/> 
       <input type="text" name="input2" /><br/> 
       <input type="submit" name="submit" value="Submit" /> 
      </form> 

     </div> 

</body></html> 
+0

あなたはさらに助けることができる持っている必要があり$_POST$POST_を交換してください! –

+2

本当にデバッグするだけです。通常、バグに遭遇したときにまずエラー報告を有効にします。しかし、あなたは何らかの理由でそれを無効にするあなたのやり方から抜け出しました: 'error_reporting(0);'。 "実行中のエラーを表示します。"何のエラー? *両方のフィールドを入力する必要があります!*? – HPierce

+0

私はコードを改訂しました。もう一度テストできますか? –

答えて

1

の予期しない終わりおそらく間違った変数名 - それはUndefined index 'input1'エラーのようなものが発生する必要があります。

はそうあなたが

$input1 = $_POST['input1']; 
$input2 = $_POST['input2']; 
+0

まだ出力はありません。文字列を確認する必要があります。 –

+0

@HPierce saisのように、 'error_reporting(0);'を 'error_reporting(E_ALL);'に置き換えてください。 'explode'関数の最初の引数はchar(空文字列)でなければなりませんので、疑問符の間にスペースを入れてください。そして、あなたは '$ inputarr1 = explode ...'を持っていますが、後で '$ inputarray1'(別の配列)を繰り返しています。 – LiTe

+0

この差分をご覧くださいhttps://www.diffchecker.com/CIkvwkQF – LiTe

関連する問題