0

可能重复:
如何使用 PHP 解析和处理 HTML?

$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 中进行处理。有人可以帮忙吗?

4

1 回答 1

0

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

于 2012-11-01T13:45:36.620 回答