0

これが私のコードです:

$text = '<div class="cgus_post"><a href="?p=15055"><div class="imgbox"><img src="/cgmedia/default.gif"></div></a>
        <h2 id="post-15055">
        <a href="?p=15055" rel="bookmark" title="Permanent Link to Willie Nelson Celebrates 80th Birthday Stoned and Auditioning for Gandalf">Willie Nelson Celebrates 80th Birthday Stoned and Auditioning for Gandalf</a></h2>
        <p>This video pretty much sums up why Willie Nelson is fucking awesome. Willie decided to celebrate his 80th birthday by recording an ‘audition’ for Peter Jackson. &nbsp;Willie wants to take the reigns from Ian McKellan in The Hobbit 2, and decided to show off his acting skills and give some of his own wizardly advice. The result is &nbsp;hilarious. Watch …&lt;/p>
        <br class="clear">
        </div>';
$dom = new DomDocument();
$dom->loadHTML($text);
$classname = 'cgus_post';
$finder = new DomXPath($dom);
$nodes = $finder->query("//*[contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");
foreach($nodes as $node){
    echo $node->nodeValue;  
}

私が抱えている問題は、クラスを含む div をクエリしてcgus_post、テキストだけを返すことです。HTML要素も返すにはどうすればよいですか?

4

1 に答える 1

0

これが、私がいつも使用している innerHTML 関数です。

function innerHTML(DOMNode $node, $trim = true, $decode = true) {
   $innerHTML = '';

   foreach ($node->childNodes as $inner_node) {
      $temp_container = new DOMDocument();
      $temp_container->appendChild($temp_container->importNode($inner_node, true));

      $innerHTML .= ($trim ? trim($temp_container->saveHTML()) : $temp_container->saveHTML());
   }

   return ($decode ? html_entity_decode($innerHTML) : $innerHTML);
}

次に、次のようにします。

$dom = new DOMDocument();
$dom->loadHTML($html);

echo htmlentities(innerHTML($dom->documentElement->childNodes->item(0)->firstChild));
于 2013-08-29T19:03:31.263 に答える