2015-08-18 25 views
0

如何顯示在循環中顯示sence的次數?在foreach循環中計算strpos適用次數

$i = 0; 
foreach ($parts as $new[$i]) { 

    $abouttoexpire = strpos($new[$i], 'Your Airbnb question is about to expire'); 
    $anairbnbexpert = strpos($new[$i], 'An Airbnb expert is waiting on feedback from you regarding'); 
    $requesttoalter = strpos($new[$i], 'Your request to alter reservation'); 

    //when the message was made 
    preg_match('/<div class="timestamp"[\s\S]*?>(.*)*?\+0000<\/div>/', $new[$i], $dateofmsg); 

    //between 9am and 6pm 
    $hour = date('H', strtotime(@$dateofmsg[1])); 
    if ($hour >= 9 && $hour <= 18) { 

     //if we found this sentence 
     if ($abouttoexpire !== FALSE) { 
     //how i show here how many times we catch the sentence @abouttoexpire?? 
     } 
    } 
} 
$i++; 

裏面的條件:if ($abouttoexpire !== FALSE) 如何打印此SENCE(@abouttoexpire)的時間循環期間計提壞賬?

回答

0
$i = 0;$j=0; 
foreach ($parts as $new[$i]) { 

    $abouttoexpire = strpos($new[$i], 'Your Airbnb question is about to expire'); 
    $anairbnbexpert = strpos($new[$i], 'An Airbnb expert is waiting on feedback from you regarding'); 
    $requesttoalter = strpos($new[$i], 'Your request to alter reservation'); 

    //when the message was made 
    preg_match('/<div class="timestamp"[\s\S]*?>(.*)*?\+0000<\/div>/', $new[$i], $dateofmsg); 

    //between 9am and 6pm 
    $hour = date('H', strtotime(@$dateofmsg[1])); 
    if ($hour >= 9 && $hour <= 18) { 

    //if we found this sentence 
    if ($abouttoexpire !== FALSE) { 
     $j++; 
    } 
    } 

} 
$i++; 

echo 'no_of_times:'.$j; 
+0

這是給我no_of_times:0 –

+0

爲什麼你需要這樣$ I ++,它的外面foreach..right? –

+0

它是需要的,因爲那裏有3個循環,但即使沒有$我會改變什麼? –

0

嘗試......

$i = 0; 
    $j = 0; 
    $k = 0; 
    foreach ($parts as $new[$i]) { 

    if ($hour >= 9 && $hour <= 18) { 

    $j++; 

    if ($abouttoexpire !== FALSE) { 

    $k++; 

     } 
    } 
    $i++; 
    } 

    echo "foreach loop=".$i; 
    echo "if loop =".$j; 
    echo "within if loop =".$k; 
+0

檢查這裏的短語在哪裏? –

+0

着understand.explain清楚 –

+0

我的意思是,如果($ abouttoexpire ==假的!)我可以看到你把它添加到代碼..您的解決方案是給我: foreach循環= 1 如果循環= 0 內如果循環= 0 foreach循環= 1 如果循環= 0 內,如果循環= 0 foreach循環= 1 如果循環= 0 內,如果循環= 0 foreach循環= 1 如果循環= 0 內,如果循環= 0 foreach loop = 1 if loop = 1 if if loop = 0 foreach loop = 1 if loop = 1 if if loop = 0 但不是確切的數字。也許是因爲這是額外的2個foreach循環內循環。但如果我有大約20個strpos來檢查我會做出20個條件和運營商。這不是一個好方法 –