2016-09-16 80 views
1

我喜欢Get DIV content from external Website的解决方案,但我需要从包含特定字符串的DIV类中找到值,例如我的例子btx764839(例如)。例如,div类的名称不是单独的字符串,而是<div class="dark btx764839">76</div>通过特定的字符串获取DIV内容

我需要脚本来搜索包含btx764839的DIV,然后再引用那个特定的DIV。我一直在用尝试很多不同的东西,但没有成功。在另一篇文章中找到的脚本下面(没有我的调整)。我希望得到一些帮助。非常感谢你!

$url = 'url'; 
$content = file_get_contents($url); 
$first_step = explode('<div class="dark btx764839">' , $content); 
$second_step = explode("</div>" , $first_step[1]); 

echo $second_step[0]; 

这是我最新的尝试:从提供的示例采取

$url = 'url'; 
$content = file_get_contents($url); 

foreach($content as $div) { 
    // Loop through the DIVs looking for one withan id of "content" 
    // Then echo out its contents (pardon the pun) 

    // get class attribute of a div, it's a string 
    $class = $div->getAttribute('class'); 

    // find substring 'btx764839' in $class 
    if (strpos($class, 'btx764839') !== false) { 
     echo $div->nodeValue; 
     } 
} 

回答

0

代码:

foreach($divs as $div) { 
    // Loop through the DIVs looking for one withan id of "content" 
    // Then echo out its contents (pardon the pun) 

    // get class attribute of a div, it's a string 
    $class = $div->getAttribute('class'); 

    // find substring 'btx764839' in $class 
    if (strpos($class, 'btx764839') !== false) { 
     echo $div->nodeValue; 
    } 
} 
+0

对不起,我不知道如何将您的代码与我相结合。代码引用另一页'$ url ='url';'的内容。 – vloryan