preg_match for nested html tags

这一生的挚爱 提交于 2019-12-20 06:36:37

问题


I would like to catch all "dev" tags and their respective content, through php preg_match_all() but can't get the nested ones.

data:

<dev>aaa</dev> <dev>bbb</dev> <dev> ccc <dev>ddd</dev> </dev>

my expression so far:

|<dev>(.*)</dev>|Uis

thanks, for your help, b.


回答1:


Don’t use regular expressions for parsing. Use a real parser like DOMDocument or SimpleXML:

$xml = simplexml_load_string('<root>'.$str.'</root>');



回答2:


You need to have a recursive matching pattern:

/<dev>(.*|(?R))<\/dev>/i

That will just suck up any nested elements, so if you want to then parse those, you will have to run the function again on $matches[1]




回答3:


The * is a greedy operator, consumes as many characters as possible. You should use the *? non-greedy version instead to find the smallest possible matches. Maybe regexes are not the best tools to do this.



来源:https://stackoverflow.com/questions/1422553/preg-match-for-nested-html-tags

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