2016-11-07 79 views
-1

我有兩種PHP格式的輸入字段。這些字段需要保存到數組中,並且輸出應該在括號中包含它旁邊每個單詞的單詞長度。還需要評估哪個輸入字段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

你能進一步幫助! –

+2

你真的只需要調試它。通常,當你遇到錯誤時,你要做的第一件事就是啓用錯誤報告。但是出於某種原因,你已經不再使用它了:'error_reporting(0);'。 「它在執行過程中顯示錯誤。」什麼錯誤? *您需要輸入兩個字段!*? – HPierce

+0

我修改了代碼。你能再次測試嗎? –

回答

1

可能是錯誤的變量名意外結束 - 這應該引起類似Undefined index 'input1'錯誤。

嘗試更換$POST_$_POST所以你應該有

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

仍然沒有輸出。需要驗證字符串爆炸並計數 –

+0

首先,作爲@HPierce sais嘗試用'error_reporting(E_ALL)'替換'error_reporting(0);'或者只是註釋該行以查看代碼中的錯誤。你的'explode'函數的第一個參數必須是char(你有空字符串),所以在問號之間放一個空格。並且比'$ inputarr1 = explode ...'要多,但後來你會遍歷'$ inputarray1'(不同的數組)。 – LiTe

+0

看看是這個區別https://www.diffchecker.com/CIkvwkQF – LiTe