How do I get the title and complete class value?

2019-09-17 03:53发布

问题:

I have two queries

1) <div class="a b" title="my title"> How do I get the title and complete class value ? ('a' always be a fixed class but not 'b'. Second class can be anything and also possiblity to have more than two classes.)

2) <h1><a link="dynamic_link" class="fix-class">my content< /a>< /h1> Get all attributes(link,class..) of 'a href' tag which belongs to only 'h1' tag

Please help me out because I stuck on this point from last two days and not able to get any solution from google. This is my first quetion on stackoverflow. I hope I get many hands for help. Thanks in advance.

回答1:

You should use XPath to query HTML or XML. To use XPath you have to load you HTML into a DOMDocument object.

/* $doc is an instance of DOMDocument */

$xpath = new DOMXPath( $doc );

$myElemnts = $xpath->query( '//li[contains(concat(" ", normalize-space(@class), " "), " classA ")]' );

The XPath class matching example is from http://www.cowburn.info/2009/06/15/xpath-css-class/

Please refer to one of the many XPath manuals on the web, to find out how to get you queries written in XPath.