2017-07-28 50 views
-2

我想從Play商店whatsapp中獲取新文本。我正在嘗試下面的代碼,它運行良好。PHP file_get_contents按類名從多個div中獲取所有內容

<?php 
$url = 'https://play.google.com/store/apps/details?id=com.whatsapp&hl=en'; 
$content = file_get_contents($url); 
$first_step = explode('<div class="recent-change">' , $content); 
$second_step = explode("</div>" , $first_step[1]); 
echo $second_step[0]; 
?> 

問題是,此代碼僅顯示來自first recent-change div類的文本。它有多個div,最近更改類名稱。如何從中獲取所有內容?

+2

[DOM日DOM DOM DOM(http://php.net/manual/en/class.domdocument.php) – CD001

+1

請仔細閱讀[什麼話題能我問](http://stackoverflow.com/help/on-topic) 和[如何問一個好問題](http://stackoverflow.com/help/how-to-ask) 和[the完美的問題](http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/) 以及如何創建[最小,完整和可驗證示例](http:// stackoverflow。 com/help/mcve) 還有[參觀](http://stackoverflow.com/tour) – RiggsFolly

+1

你有使用domdocument導航與xpath! – MikeSouto

回答

1

正如在評論中已經建議你必須使用dom內容。但是如果你想顯示包含recent-change類的所有文本。你可以使用循環。我提供的同樣的方法解決方案,您使用

$url = 'https://play.google.com/store/apps/details?id=com.whatsapp&hl=en'; 
$content = file_get_contents($url); 
$first_step = explode('<div class="recent-change">' , $content); 
foreach ($first_step as $key => $value) { 
    if($key > 0) 
    { 
    $second_step = explode("</div>" , $value); 
    echo $second_step[0];  
    echo "<br>"; 
    } 
} 
相關問題