使用简单的HTML Dom PHP

使用简单的HTML Dom PHP,php,html,dom,Php,Html,Dom,好的,我有点问题,希望有人能帮我解决 我正在使用简单的HTMLDOM 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 {

好的,我有点问题,希望有人能帮我解决

我正在使用简单的HTMLDOM 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 = '';
}
现在,在这个代码获取信息的站点上,有两个与下面的示例相同

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

<div class="inline">
    <h3>Run Time:</h3>
    <span>120 min</span>
</div>
现在我只想抓住国家,但有时国家不可用,它最终得到运行时间,所以我可以使用它吗

<h3>Country:</h3>
要停止此操作,请有人帮助我。谢谢。

设置:

$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);
使用css,您可以实现以下目标:

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