简体   繁体   中英

PHP Simple HTML DOM

I'm trying to access a H1 tag from a html source I am retrieving with PHP CURL.

The HTML: http://pastebin.com/4ubAhS3E

I'm trying to access the last line (line 63): <h1>Your referrals (5 complete / 0 pending / 9999 total)</h1>

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>';

But it's just bringing back the whole HTML

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)');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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