dongshao5573 2016-01-22 06:21
浏览 75

如何使用php从图像链接中提取'href'和'src'?

have a code like this:

<a href='www.link_not_required.com'>
<a href='www.link_not_required.com'>
<a href='www.link_1.com'><img src='image_1.png'></a> 
<a href='www.link_2.com'><img src='image_2.png'></a> 
<a href='www.link_3.com'><img src='image_3.png'></a> 
<a href='www.link_4.com'><img src='image_4.png'></a> 
<img src='image_not_required.png'>
<img src='image_not_required.png'>

I want to extract hrefs of only anchors which contain images and also src of those images ? I don't want links of anchors which do not contain images and also srcs of images which are not anchors.

How do I do this ? Can it be done using Simplehtmldom library?

  • 写回答

2条回答 默认 最新

  • dounabi6295 2016-01-22 06:33
    关注

    I'm not sure why would you want to access contents of a HTML page using PHP which is a server side language. You could easily do this using JavaScript or jQuery.

    However, lets say you read the contents of the HTML file/URL using some method (some of them can be file_get_contents, cURL, readfile etc.), and wish to use SimpleHTMLDom library. You could do below

    1. find all the images in the page and loop through them
    2. find the parent element of the selected item from above

    Step #1 will give you all img tags, while step #2 will give you the corresponding parent anchor tags. You should be able to extract the required attributes.

    All of this is available at http://simplehtmldom.sourceforge.net/manual.htm and I don't think Googling/reading through manual is that difficult.

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号