2014-09-13 2 views
-3

누군가가 내가 루프의 총을 얻으 려하고 있습니다.PHP에서 루프의 총 얻기

Heres는 내 코드

echo '<form name="fproduct" action="product.php" method="post"> 
<br/><textarea name="item" rows="10" cols="20"></textarea> <br/> 
<input type="submit" value="Submit" /> 
</form>'; 

if(isset($_POST['item'])) { 
    $j = 0; 
    $arry=explode("\\r\\n", $_POST['item']); 
    for ($i = 0; $i <= count($arry); $i++) { 
     if((trim($arry[$i])) != null) { 
      $j++; 
     } 
    } 
    Print $j; 
} 

뭐죠이 문제.

+1

는'에코 $ j를 시도이의' –

+0

뭐죠 문맥을 :

if(isset($_POST['item'])){ $j = 0; $arry=explode("\n", $_POST['item']); for ($i = 0; $i < count($arry); $i++) { if((trim($arry[$i]))!= null){ $j++; } } echo $j; } 

내가 사용 foreach을 제안? 개행 문자로 내부에있는 항목의 수를 세는 중입니까? 하지만 빈 공간을 무시합니까? – Ghost

+1

@CodeBaba [ "print는 실제로는 실제 함수가 아닙니다 (언어 구조이므로) 인자 목록에 괄호를 사용할 필요가 없습니다."] (http://php.net/manual/en/function.print .php) –

답변

0

대신 \n을 사용해보세요. `인쇄 ($ j를) 같은`인쇄 작업;

if(isset($_POST['item'])){ 
    $count = 0; 
    $item = explode("\n", $_POST['item']); 
    foreach($item as $line) { 
     if(trim($line) != '') $count++; 
    } 
    echo $count; 
} 
+0

감사합니다. foreach에서 작동합니다. –

+0

@JuliusMalundras – Ghost