-1

可能重复:
计算页面中的所有 HTML 标签 PHP
如何使用 PHP 解析和处理 HTML?

我在解析信息以检索 HTML 文档中使用的 html 和元标记列表以及每个标记在文档中出现的次数时遇到问题。

因此,例如,如果我有以下 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
4

1 回答 1

2

请参阅http://www.php.net/manual/en/class.domdocument.php以解析 PHP 中的 DOM(例如,从方法http://www.php.net/manual/en/domdocument.loadhtml .php)。有关任何其他 API,另请参阅http://www.php.net/manual/en/book.dom.php

于 2012-10-02T02:04:57.860 回答