2017-07-28 66 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>"; 
    } 
} 
相关问题