Extract all content (including HTML) from a div cl

2019-02-25 20:33发布

问题:

Example HTML...

<html>
<head></head>
<body>
<table>
<tr>
    <td class="rsheader"><b>Header Content</b></td>
</tr>
<tr>
    <td class="rstext">Some text (Most likely will contain lots of HTML</td>
</tr>
</table>
</body>
</html>

I need to convert a page of HTML into a templated version of that HTML page. The HTML page is made up of several boxes, each with a header (refered to in the above code as "rsheader") and some text (refered to in the above code as "rstext").

I'm trying to write a PHP script to retrieve the HTML page maybe using file_get_contents and then to extract whatever content is within the rsheader and rstext divs. Basically I don't know how to! I've tried experimenting with DOM but I don't know it too well and although I did manage to extract the text, it ignored any HTML.

My PHP...

<?php

$html = '<html>
<head></head>
<body>
<table>
<tr>
    <td class="rsheader"><b>Header Content</b></td>
</tr>
<tr>
    <td class="rstext">Some text (Most likely will contain lots of HTML</td>
</tr>
</table>
</body>
</html>';

$dom = new DomDocument();
$dom->loadHtml($html);
$xpath = new DomXpath($dom);
$div = $xpath->query('//*[@class="rsheader"]')->item(0);
echo $div->textContent;

?>

If I do a print_r($div) I see this...

DOMElement Object
    (
    [tagName] => td
    [schemaTypeInfo] => 
    [nodeName] => td
    [nodeValue] => Header Content
    [nodeType] => 1
    [parentNode] => (object value omitted)
    [childNodes] => (object value omitted)
    [firstChild] => (object value omitted)
    [lastChild] => (object value omitted)
    [previousSibling] => 
    [nextSibling] => (object value omitted)
    [attributes] => (object value omitted)
    [ownerDocument] => (object value omitted)
    [namespaceURI] => 
    [prefix] => 
    [localName] => td
    [baseURI] => 
    [textContent] => Header Content
    )

As you can see there are no HTML tags within the textContent node which leaves me to believe I'm going about it the wrong way :(

Really hoping someone might be able to give me some help with this...

Thanks in advance

Paul

回答1:

X-Path is probably a bit more a sledgehammer than you need for this task. I would try using DOMDocument's getElementById() method instead. An example is below, which was adapted from this post.

NOTE: Updated to use tag and class names instead of element IDs.

function getChildHtml( $node ) 
{
    $innerHtml= '';
    $children = $node->childNodes;

    foreach( $children as $child )
    {
        $innerHtml .= sprintf( '%s%s', $innerHtml, $child->ownerDocument->saveXML( $child ) );
    }

    return $innerHtml;
}

$dom = new DomDocument();
$dom->loadHtml( $html );

// Gather all table cells in the document.
$cells = $dom->getElementsByTagName( 'td' );

// Loop through the collected table cells looking for those of class 'rsheader' or 'rstext'.
foreach( $cells as $cell )
{
    if( $cell->getAttribute( 'class' ) == 'rsheader' )
    {
        $headerHtml = getChildHtml( $cell );
        // Do something with header html.
    }

    if( $cell->getAttribute( 'class' ) == 'rstext' )
    {
        $textHtml = getChildHtml( $cell );
        // Do something with text html.
    }
}


回答2:

Look at this answer and use it as guideline: retrieving specific data from a website

If you need detailed help I am here to help.



标签: php dom extract