-2

Does any one know how to extract only div tags with particular class,in PHP?

<div class="abc">
</div>
<div class="def">
</div>
<div class="xyz">
</div>

I need div only with class="abc". How can I implement that?

2

1 Answer 1

1

What you want is: XPath

<?php
$html = new DOMDocument();
@$html->loadHtmlFile('thepage.html');
$xpath = new DOMXPath( $html );
$nodelist = $xpath->query( "//*[contains(@class, 'abc')]" );
foreach ($nodelist as $n){
  echo $n->nodeValue."\n";
}
?>

[updated]

Added new xpath query. give that a try.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the answer. But its giving not giving me the results that I need. <br> DOMNodeList Object ( [length] => 0 )
is the file located on the same server? or a remote file?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.