2016-11-07 5 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

당신이 더 도와 줄 수 있어야한다 $_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