PHP简单HTML DOM [英] PHP Simple HTML DOM
本文介绍了PHP简单HTML DOM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从正在使用PHP CURL检索的html源中访问H1标签。
I'm trying to access a H1 tag from a html source I am retrieving with PHP CURL.
HTML: http://pastebin.com/4ubAhS3E
我正在尝试访问最后一行(第63行):< h1>您的推介链接(5个完整/ 0个待处理/总计9999个)< / h1>
I'm trying to access the last line (line 63): <h1>Your referrals (5 complete / 0 pending / 9999 total)</h1>
这是php简单html dom使用的代码部分
Here is the part of the code that the php simple html dom is using
$result1 = curl_exec ($ch1);
curl_close($ch1);
echo $result1;
$html = str_get_html($result1);
$main = $html->find('h1', 3);
foreach($main as $element)
echo $element->innertext . '<br>';
但这只是恢复了整个HTML
But it's just bringing back the whole HTML
推荐答案
使用DOMDocument很容易,您可以获取元素内的文本内容:
It is easy with DOMDocument, you can fetch the text content inside an element:
$result1 = curl_exec ($ch1);
curl_close($ch1);
echo $result1;
$dom = new DOMDocument();
$dom->loadHTML($result1);
$xpath = new DOMXpath($dom);
echo $xpath->evaluate('string(//section[@id="your_referrals"]/h1)');
这篇关于PHP简单HTML DOM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文