1

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.

1
  • You should post the html with your question rather than the curl code. Commented Oct 23, 2014 at 0:42

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

2 Comments

for the simple example I had, your solution worked. but if I change the URL link to same structure, it doesn't work anymore. Actually, I make a simple example out of the URL that I want to parse with exact same structure but simplified. Care to see the URL?
0

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>

4 Comments

@MIvanlsten, I'v tried it but still its returning NULL.
are you sure you have the 123456 in that span? maybe the spam is empty so obviously no result is returned...
@AresDraguna Yes. please inspect this url relumastudio.com/test/target.html
what does $html->find('span[class=mynum]',0)->innertext; return?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.