anagnam
anagnam

Reputation: 143

PHP Simple HTML DOM parser - parsing nested elements

I've been playing with PHP Simple HTML DOM Parser Manual found here http://simplehtmldom.sourceforge.net/manual.htm and I got success with some tests except this one:

It got nested tables and spans and I would like to parse the outer text of span with class of mynum.

<?php

require_once 'simple_html_dom.php';

$url = 'http://relumastudio.com/test/target.html';
$ch = curl_init();
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.21 (KHTML, like Gecko) Chrome/19.0.1042.0 Safari/535.21");
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);

$DEBUG = 1;

if($DEBUG){
    $html = new simple_html_dom();
    $html->load($url);
    echo $html->find('span[class=mynum]',0)->outertext; // I should get 123456
}else{
    echo $result;
}        
curl_close($ch);

I thought I could get away with just once call to echo $html->find('span[class=mynum]',0)->outertext; to get the text 123456 but I can't.

Any ideas? Any help is greatly appreciated. Thank You.

Upvotes: 1

Views: 2167

Answers (2)

Kevin
Kevin

Reputation: 41885

Load the url properly first. Then use ->innertext in this case:

$url = 'http://relumastudio.com/test/target.html';
$html = file_get_html($url);
$num = $html->find('span.mynum', 0)->innertext;
echo $num;

Upvotes: 1

MIvanIsten
MIvanIsten

Reputation: 490

You need innertext.

$html = new simple_html_dom();
$html->load_file($url);
echo $html->find('span[class=mynum]',0)->innertext;

outertext returns <span class="mynum">123456</span>

Upvotes: 0

Related Questions