J van Amerongen
J van Amerongen

Reputation: 67

Get contents of element from external page PHP

I'd like to get the content (CSS, children, ect.) to display on a HTML page, but this element is on a external page. When I use:

$page = new DOMDocument();
$page->loadHTMLFile('about.php');
$text = $page->getElementById('text');
echo $text->nodeValue;

I only get the text, but #text also has a image as child and some CSS. Can I get (and echo) those to, kind of like with an iframe, but then with a element. If so, how?

Thanks a lot.

Upvotes: 1

Views: 1521

Answers (2)

martin
martin

Reputation: 96959

Maybe what you're looking for is DOMDocument::saveHTML().

If you set the optional arguments it outputs only this particular node.

$elm = $page->getElementById('text');
echo $elm->ownerDocument->saveHTML($elm);

Upvotes: 1

J van Amerongen
J van Amerongen

Reputation: 67

I have found a solution, although it doesn't retrieve the CSS, but if you only need the element and its children, this is my best bet.

Use simple_html_dom.php to do all the hard stuff.

My external page:

<div id='text'>
    <img src='img/dummy.png' align='left' alt='Image not available. Our apologies.'/>
    <span>text</span><br/>
    <p>
        text
    </p>
    <p>
       text
    </p>
    <p>
        text
    </p>
<div>

Now, my page that I'd like to show the contents of my external page:

<?php include('../includes/simple_html_dom.php'); ?>
....
<?php
    $html = file_get_html('about.php');
    $ret = $html->find('div#text', 0);
    echo $ret;
?>

what this does, it echos the element with its children, without CSS unfortunately.

Upvotes: 0

Related Questions