我需要获取锚标记的文本和链接,即锚标记的href。我需要在php中进行处理

时间:2012-11-01 13:31:53

标签: php html

  

可能重复:
  How to parse and process HTML with PHP?

$strHTML = '<div>
<h2 class="entry-title">
<a rel="bookmark" title="Permalink to BSR Fuels Report; Rezidor Hotels to Reduce Energy; Germany Caps Subsidies &ndash; 3BL Media Energy Minute" href="http://www.caelusgreenroom.com/2012/10/17/bsr-fuels-report-rezidor-hotels-to-reduce-energy-germany-caps-subsidies-3bl-media-energy-minute/">BSR Fuels Report; Rezidor Hotels to Reduce Energy; Germany Caps Subsidies &ndash; 3BL Media Energy Minute</a>
</h2>

<h2 class="entry-title">
<a rel="bookmark" title="Permalink to Small Business Healthcare Costs to Decrease; EU Pharma Guidelines &ndash; 3BL Media" href="http://www.caelusgreenroom.com/2012/10/17/small-business-healthcare-costs-to-decrease-eu-pharma-guidelines-3bl-media/">Small Business Healthcare Costs to Decrease; EU Pharma Guidelines &ndash; 3BL Media</a>
</h2>
</div>';

我将上面的html作为php变量中的字符串,如图所示。我需要获取锚标记的文本和链接,即锚标记的href。我需要在php中进行处理。有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

使用DomDocument解析字符串,然后使用getElementsByTagName函数检索所有a元素。然后,您可以使用foreach迭代它们并检索所需的数据。查看DomElement以获取可能的属性和方法列表。