douchao1879 2012-11-01 13:31
浏览 23
已采纳

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

Possible Duplicate:
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>';

I have the above html as a string in a php variable as shown. I need to get the text of the anchor tag and the link i.e. href of the anchor tag. I need to do the processing in php. Can anyone please help?

  • 写回答

1条回答 默认 最新

  • dongtiao2105 2012-11-01 13:45
    关注

    Use DomDocument to parse the String and then use getElementsByTagName function to retrieve all the a elements. Then you can just iterate over them using foreach and retrieve the data you want. Check out DomElement for a list of possible properties and methods.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)