且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用PHP从div类中提取所有内容(包括HTML)

更新时间:2023-09-03 17:54:46

X-Path可能比这个任务要多一点,我会尝试使用DOMDocument的 getElementById()方法,下面的例子是从这篇文章

注意:更新为使用标签和类名而不是元素ID。 >

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.
    }
}