2012-10-02 156 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

回答