How do I get the title and complete class value?

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-17 03:50:21

问题


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.



来源:https://stackoverflow.com/questions/25348936/how-do-i-get-the-title-and-complete-class-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!