【问题标题】:extracting and printing an html element by it's id using DOMDocument使用 DOMDocument 通过 id 提取和打印 html 元素
【发布时间】:2012-06-27 12:02:53
【问题描述】:

我想从网页中提取几个表格并在我的页面中显示它们

我打算使用正则表达式来提取它们,但后来我看到了 DOMDocument 类 我在stackoverflow中查看过似乎更干净,似乎所有问题都是关于获取内部文本或使用循环来获取元素的内部节点。我现在想知道如何通过它的 id 提取和打印 html 元素。

$html = file_get_contents("www.site.com");
$xml = new DOMDocument();
$xml->loadHTML($html);
$xpath = new DOMXPath($xml);
$table =$xpath->query("//*[@id='myid']");
$table->saveHTML(); // this obviously doesn't work

如何在我的页面上将 $table 显示或回显为实际的 html 表格?

【问题讨论】:

    标签: php domdocument


    【解决方案1】:

    首先,DOMDocument 有一个 getElementById() 方法,因此您的 XPath 是不必要的 - 尽管我怀疑它在下面是如何工作的。

    其次,为了获取标记片段而不是整个文档,您使用DOMNode::C41N(),因此您的代码将如下所示:

    <?php
    
        // Load the HTML into a DOMDocument
        // Don't forget you could just pass the URL to loadHTML()
        $html = file_get_contents("www.site.com");
        $dom = new DOMDocument('1.0');
        $dom->loadHTML($html);
    
        // Get the target element
        $element = $dom->getElementById('myid');
    
        // Get the HTML as a string
        $string = $element->C14N();
    

    查看working example

    【讨论】:

    • 谢谢。我对选择有疑问。如果我想获得具有特定 id 的 div 中的第一个表,我应该怎么做?类似于 $element = $dom->getElementById('mydiv')->firstchild('table'); ??
    • 我会做$element = $dom-&gt;getElementById('mydiv')-&gt;getElementsByTagName('table')-&gt;item(0); - firstChild 是一个属性,而不是一个方法。如果您想要的表格是 div 的第一个子元素,您可以使用firstChild,但getElementsByTagName() 更安全。就方法名称和机制而言,这与 Javascript 相同,除了 PHP item(),因为 PHP 在 5.4 之前不支持 array dereferencing
    【解决方案2】:

    您可以使用 DOMElement::C14N() 来获取 DOMElement 的规范化 HTML(XML) 表示,或者如果您喜欢更多控制以便过滤某些元素和属性,您可以使用以下内容:

    function toHTML($nodeList, $tagsToStrip=array('script','object','noscript','form','style'),$attributesToSkip=array('on*')) {
    $html = '';
    foreach($nodeList as $subIndex => $values) {
        if(!in_array(strtolower($values->nodeName), $tagsToStrip)) {
            if(substr($values->nodeName,0,1) != '#') {
                $html .= ' <'.$values->nodeName;
                if($values->attributes) {
                    for($i=0;$values->attributes->item($i);$i++) {
                        if( !in_array( strtolower($values->attributes->item($i)->nodeName) , $attributesToSkip ) && (in_array('on*',$attributesToSkip) && substr( strtolower($values->attributes->item($i)->nodeName) ,0 , 2) != 'on') ) {
                            $vvv = $values->attributes->item($i)->nodeValue;
                            if( in_array( strtolower($values->attributes->item($i)->nodeName) , array('src','href') ) ) {
                                $vvv = resolve_href( $this->url , $vvv );
                            }
                            $html .= ' '.$values->attributes->item($i)->nodeName.'="'.$vvv.'"';
                        }
                    }
                }
                if(in_array(strtolower($values->nodeName), array('br','img'))) {
                    $html .= ' />';
                } else {
                    $html .= '> ';
                    if(!$values->firstChild) {
                        $html .= htmlspecialchars( $values->textContent , ENT_COMPAT , 'UTF-8' , true );
                    } else {
                        $html .= toHTML($values->childNodes,$tagsToStrip,$attributesToSkip);
                    }
                    $html .= ' </'.$values->nodeName.'> '; 
                }
            } elseif(substr($values->nodeName,1,1) == 't') {
                $inner = htmlspecialchars( $values->textContent , ENT_COMPAT , 'UTF-8' , true );
                $html .= $inner;
            }
        }
    }
    return $html;
    }
    
    echo toHTML($table);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-10
      • 1970-01-01
      • 2015-03-04
      • 2021-06-16
      • 2021-05-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多