2

次のコードを使用して、別の Web サイトから div を抽出しようとしています。

<?php

    $doc = new DomDocument;

     //We need to validate our document before refering to the id
    $doc->validateOnParse = true;
    $doc->loadHtml(file_get_contents('http://myaddresshere'));

    var_dump($doc->getElementById('the div'));



?>

結果を取得できますが、その前に長いphpコード文字列も取得しています:

object(DOMElement)#2 (18) { ["tagName"]=> string(2) "h3" ["schemaTypeInfo"]=> NULL ["nodeName"]=> string(2) "h3" ["nodeValue"]=> string(35) "Telephone Technical Support: Active" ["nodeType"]=> int(1) ["parentNode"]=> string(22) "(object value omitted)" ["childNodes"]=> string(22) "(object value omitted)" ["firstChild"]=> string(22) "(object value omitted)" ["lastChild"]=> string(22) "(object value omitted)" ["previousSibling"]=> string(22) "(object value omitted)" ["nextSibling"]=> string(22) "(object value omitted)" ["attributes"]=> string(22) "(object value omitted)" ["ownerDocument"]=> string(22) "(object value omitted)" ["namespaceURI"]=> NULL ["prefix"]=> string(0) "" ["localName"]=> string(2) "h3" ["baseURI"]=> NULL ["textContent"]=> string(35) ***"Telephone Technical Support: Active"*** }

その文字列をすべて削除して、div のコンテンツのみを取得するにはどうすればよいですか。

  • 問題の div は、ブール値の結果 (アクティブ/期限切れ) を示しています
4

1 に答える 1

0

ノードの値は、次の 2 つの方法で取得できます。

DomElement->nodeValue; // inherited from DomNode

または経由

<?php 
function get_inner_html( $node ) { 
    $innerHTML= ''; 
    $children = $node->childNodes; 
    foreach ($children as $child) { 
        $innerHTML .= $child->ownerDocument->saveXML( $child ); 
    } 

    return $innerHTML; 
} 
?>

詳細については、http: //php.net/manual/en/class.domelement.phpおよびhttp://www.php.net/manual/en/class.domnode.phpを参照してください。

于 2013-11-06T13:55:24.407 に答える