2012-10-02 158 views
-1

可能重复:
Count all HTML tags in page PHP
How to parse and process HTML with PHP?解析HTML检索标签

我有解析信息检索在HTML文档中使用HTML和meta标签的列表麻烦以及每个标签在文档中出现的次数。

因此,举例来说,如果我有以下的HTML文档

<head> 
<a href="example.com">example1</a> 
<a href="example.com">example2</a> 
<a href="example.com">example3</a> 
</head> 

然后你会得到一个列表就像

head tag =1 
a tag =3 

我想用PHP来做到这一点,如果有人甚至可以给我一个很好的起点。

编辑: 我试图复制类似下面的Python代码,但用PHP

class MyHTMLParser(HTMLParser): 
    def handle_starttag(self, tag, attrs): 
    print "Encountered a start tag:", tag 
    def handle_endtag(self, tag): 
    print "Encountered an end tag :", tag 
    def handle_data(self, data): 
print "Encountered some data :", data 
+1

从某种意义上说,是的,我确实阅读过这篇文章,但我更多的是在识别何时出现html标签时遇到麻烦。我正在寻找类似于例19.1.1的python代码,在以下链接:http://docs.python.org/library/htmlparser.html但似乎无法弄清楚如何执行类似的任务php – user1422508

回答