1

I've searched around for solutions to this question, but each one i find, and try doesn't work.

I'm trying to grab the content of a div from a forum topic.

I've tried using preg_match and that only displayed "Array" then I tried using this method

$html = file_get_contents("http://www.lcs-server.co.uk/forum/index.php/topic,$id_topic");
$dom = new DOMDocument;
$dom->loadHTML($html);
$element = $dom->getElementById("msg_$id_msg");
var_dump($element);

This will show "object(DOMElement)#1 (0) { } "

The $id_topic and $id_msg are defined above this code, taken from the forum database. I did try taking the message from the forum database, but it displayed BB code tags, I'd like it to grab the post content, and display it in HTML, as it's displayed on the forum post itself.

This is the code I'm using now and giving me "Fatal error: Cannot redeclare DOMinnerHTML()"

$html = file_get_contents("http://www.lcs-server.co.uk/forum/index.php/topic,$id_topic");

$dom = new DOMDocument;
$dom->loadHTML($html);
$domelement = $dom->getElementById("msg_$id_msg");

foreach ($domelement as $element) 
{ 
    echo DOMinnerHTML($element); 
} 

function DOMinnerHTML($DOMelement) 
{ 
$innerHTML = ""; 
    $children = $DOMelement->childNodes; 
    foreach ($children as $child) 
    { 
            $tmp_dom = new DOMDocument(); 
        $tmp_dom->appendChild($tmp_dom->importNode($child, true)); 
        $innerHTML.=trim($tmp_dom->saveHTML()); 
    } 
        return $innerHTML; 
    }
SAFC
  • 314
  • 1
  • 3
  • 11
  • Are you absolutely sure that msg_ exists when you're trying to retrieve it? It's possible that your $id_msg retrieval method is broken. – Will Jan 02 '12 at 02:23
  • Yeah it exists, I was using it to create a link to the actual post in the forum, from the page I'm displaying it on. – SAFC Jan 02 '12 at 14:18
  • What are you trying to do? Why do you need to display a forum post on another page? Is it on the same domain, or a different website (i.e. are you scraping content)? – Martin Bean Jan 02 '12 at 14:26
  • I'm making it a little like a blog, where I post a topic in a certain forum board, and it displays the topic on the home page, in a table format, but it doesn't really matter why I want to, I'm just posting here for help on how to do it. – SAFC Jan 02 '12 at 15:10

1 Answers1

2

getElementById returns a DOM node object. It does not return the HTML of the node. For that, you have to get the node's "innerHTML". This properly is not officially supported by PHP's dom object for some reason, but can be faked using this answer: How to get innerHTML of DOMNode?

Community
  • 1
  • 1
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • That's annother method. But it'd be nice if phpdom had `node.innerHTML` as an attribute like you have in javascript. – Marc B Jan 02 '12 at 04:35
  • This method worked almost perfectly, until i tried to enable it to grab multiple posts, from different topics. It gave me a fatal error Fatal error: Cannot redeclare DOMinnerHTML() – SAFC Jan 02 '12 at 14:20
  • You can't define functions inside a loop like that. It'll work for one iteration, then you hit the redefinition problem. Put the function definition OUTSIDE of the loop. – Marc B Jan 02 '12 at 15:33
  • AH! Yeah, how silly of me. I've got it working now, thanks everyone for the replies. – SAFC Jan 02 '12 at 17:13