简体   繁体   English

使用简单的HTML Dom PHP

[英]Using Simple HTML Dom PHP

Ok i have a bit of a problem and hope someone can help me out. 好的,我有一个问题,希望有人可以帮助我。

I'm using Simple HTML Dom PHP Class and trying to grab info from another site example below. 我正在使用简单HTML Dom PHP类,并尝试从下面的另一个网站示例中获取信息。

$html = file_get_html("http://example.com");
$find_country = $html->find('div[class=inline] span', 0);
if (!empty($find_country)) {
    $country = $find_country->plaintext;
} else {
    $country = '';
}

Now on the site where this code gets the info from there are two that are the same example below 现在在此代码从中获取信息的网站上,有两个下面的示例相同

<div class="inline">
    <h3>Country:</h3>
    <span>USA</span>
</div>

<div class="inline">
    <h3>Run Time:</h3>
    <span>120 min</span>
</div>

Now i only want to grab the Country but some times the Country is not available and it ends up getting the run time so could i use 现在我只想获取国家/地区,但有时国家/地区不可用,最终导致运行时间增加,所以我可以使用

<h3>Country:</h3>

Part to stop this could someone please help me out thanks. 阻止这部分可以有人请帮助我谢谢。

The setup: 设置:

$html = <<<EOF
<div class="inline">
    <h3>Run Time:</h3>
    <span>120 min</span>
</div>

<div class="inline">
    <h3>Country:</h3>
    <span>USA</span>
</div>
EOF;

$doc = str_get_html($html);

Using simple you need to jump through some hoops 使用简单,您需要跳过一些箍

foreach($doc->find('h3') as $h3){
  if($h3->text() == 'Country:'){
    $country = $h3->next_sibling()->text();
    break;
  }
}

Using advanced you can get there with css: 使用高级,您可以使用CSS到达那里:

$country = $doc->find('h3[text="Country:"] + span', 0)->text();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM