Page 1 of 1
PHP code to traverse through a HTML file to find all the ima
Posted: Thu Sep 02, 2010 2:19 pm
by aditya16in
I wanted a php script to fetch all the images in HTML code and list it. Can anyone help me do this, or some Idea as to how I should proceed ? I am new to this, so any help would be appreciated
Re: PHP code to traverse through a HTML file to find all the
Posted: Thu Sep 02, 2010 2:29 pm
by TipPro
You are not being very descriptive. For example where is the html coming from? Do you want to be able to collect the html out from any Web page or is this an html snippet stored on your computer, etc...
Can you at least put forth your attempt at a plan of how you think you would go about this.
Re: PHP code to traverse through a HTML file to find all the
Posted: Mon Sep 06, 2010 8:55 am
by rosbaldeston
Here's a simple script to get you started:
Code: Select all
$url = 'http://www.bbc.co.uk';
$html = strtolower(file_get_contents($url)); // Download the HTML page
$p1 = strpos($html, '<img'); // Loop through the HTML, looking for the start of an img tag
while ($p1 !== FALSE) {
$p2 = strpos($html, 'src="', $p1); // find the position of the img src attribute
$p3 = strpos($html, '"', $p2 + 5); // find the position of the closing quotes around the src attribute
$src = substr($html, $p2 + 5, $p3 - $p2 - 5); // extract the src arttribute
echo($src . '<br />'); // display it on screen
$p1 = strpos($html, '<img', $p3); // get the next value of $p1 to continue the loop
}
And the result:
Code: Select all
http://static.bbc.co.uk/frameworks/barlesque/1.4.8/desktop/2.5/img/blocks.png
http://static.bbc.co.uk/homepage/ic/mediazone/strapthumbnail/www/image/p009vfl0_640_215.jpg
http://static.bbc.co.uk/homepage/ic/mediazone/strapthumbnail/www/image/p009vc0k_640_215.jpg
http://static.bbc.co.uk/homepage/ic/mediazone/strapthumbnail/www/image/p009t1vf_640_215.jpg
http://static.bbc.co.uk/homepage/ic/mediazone/strap/www/image/p009vfl0_640_215.jpg
http://static.bbc.co.uk/homepage/ic/mediazone/strap/www/image/p009vc0k_640_215.jpg
http://static.bbc.co.uk/homepage/ic/mediazone/strap/www/image/p009t1vf_640_215.jpg
http://static.bbc.co.uk/homepage/img/expanded.png
http://static.bbc.co.uk/homepage/img/x.gif
http://news.bbcimg.co.uk/media/images/48980000/jpg/_48980700_newsofworldpic.jpg
http://static.bbc.co.uk/homepage/img/plus.gif
http://static.bbc.co.uk/homepage/img/minus.gif
http://static.bbc.co.uk/homepage/img/expanded.png
http://static.bbc.co.uk/homepage/img/x.gif
http://static.bbc.co.uk/homepage/ic/news/hero/48996000/jpg/_48996329_rooney_pa226.jpg
http://static.bbc.co.uk/homepage/img/plus.gif
http://static.bbc.co.uk/homepage/img/minus.gif
http://static.bbc.co.uk/homepage/img/plus.gif
http://static.bbc.co.uk/homepage/img/minus.gif
http://static.bbc.co.uk/homepage/img/expanded.png
http://static.bbc.co.uk/homepage/img/x.gif
http://static.bbc.co.uk/homepage/img/plus.gif
http://static.bbc.co.uk/homepage/img/minus.gif
http://static.bbc.co.uk/homepage/img/expanded.png
http://static.bbc.co.uk/homepage/img/x.gif
http://news.bbcimg.co.uk/media/images/48993000/jpg/_48993865_weller2_226pa.jpg
http://static.bbc.co.uk/homepage/img/plus.gif
http://static.bbc.co.uk/homepage/img/minus.gif
http://static.bbc.co.uk/homepage/img/expanded.png
http://static.bbc.co.uk/homepage/img/x.gif
http://newsimg.bbc.co.uk/weather/img/symbols/57x57/12.gif
http://newsimg.bbc.co.uk/weather/img/symbols/57x57/14.gif
http://newsimg.bbc.co.uk/weather/img/symbols/57x57/10.gif
http://static.bbc.co.uk/homepage/img/expanded.png
http://static.bbc.co.uk/homepage/img/x.gif
http://static.bbc.co.uk/homepage/img/expanded.png
http://static.bbc.co.uk/homepage/img/x.gif
http://news.bbcimg.co.uk/media/images/48993000/jpg/_48993471_010117795-1.jpg
http://static.bbc.co.uk/homepage/img/plus.gif
http://static.bbc.co.uk/homepage/img/minus.gif
http://static.bbc.co.uk/homepage/img/expanded.png
http://static.bbc.co.uk/homepage/img/x.gif
http://static.bbc.co.uk/homepage/img/cbbc.png
http://static.bbc.co.uk/homepage/img/cbeebies.png
http://static.bbc.co.uk/homepage/img/expanded.png
http://static.bbc.co.uk/homepage/img/x.gif
/food/images/food_4x3_146/recipes/paul_hollywoods_scones_70005_4x3.jpg
http://static.bbc.co.uk/homepage/img/plus.gif
http://static.bbc.co.uk/homepage/img/minus.gif
http://static.bbc.co.uk/homepage/img/expanded.png
http://static.bbc.co.uk/homepage/img/x.gif
http://static.bbc.co.uk/homepage/img/expanded.png
http://static.bbc.co.uk/homepage/img/x.gif
http://static.bbc.co.uk/homepage/ic/iplayer/carousel/node2/episode/b00tlmq7_640_360.jpg
http://static.bbc.co.uk/homepage/ic/iplayer/carousel/node2/episode/b00gq73d_640_360.jpg
http://static.bbc.co.uk/homepage/ic/iplayer/carousel/node2/episode/b00tqh0b_640_360.jpg
http://static.bbc.co.uk/homepage/ic/iplayer/carousel/node2/episode/b00tmz6p_640_360.jpg
http://static.bbc.co.uk/homepage/ic/iplayer/carousel/node2/episode/b00tmz7j_640_360.jpg
http://static.bbc.co.uk/homepage/ic/iplayer/carousel/node1/episode/b00thdzc_640_360.jpg
http://static.bbc.co.uk/homepage/ic/iplayer/carousel/node2/episode/b00kq2lq_640_360.jpg
http://static.bbc.co.uk/homepage/ic/iplayer/carousel/node2/episode/b00j08h7_640_360.jpg
http://static.bbc.co.uk/homepage/ic/iplayer/carousel/node2/episode/b00gd1rn_640_360.jpg
http://static.bbc.co.uk/homepage/ic/iplayer/carousel/node2/episode/b00tqnwh_640_360.jpg
http://static.bbc.co.uk/homepage/img/plus.gif
http://static.bbc.co.uk/homepage/img/minus.gif
http://static.bbc.co.uk/homepage/img/expanded.png
http://static.bbc.co.uk/homepage/img/x.gif
http://static.bbc.co.uk/homepage/ic/onnow/hero/episode/b007blcr_640_360.jpg
http://static.bbc.co.uk/homepage/img/expanded.png
http://static.bbc.co.uk/homepage/img/x.gif
http://static.bbc.co.uk/music/images/promotions/366x206/qpvh.jpg
http://static.bbc.co.uk/music/images/promotions/366x206/8mcg.jpg
http://static.bbc.co.uk/music/images/promotions/366x206/834z.jpg
http://static.bbc.co.uk/music/images/promotions/366x206/42rj.jpg
http://static.bbc.co.uk/music/images/promotions/366x206/pvhf.jpg
http://static.bbc.co.uk/music/images/promotions/366x206/6gx2.jpg
http://static.bbc.co.uk/music/images/promotions/366x206/89nv.jpg
http://static.bbc.co.uk/music/images/promotions/366x206/5pvh.jpg
http://static.bbc.co.uk/music/images/promotions/366x206/dz6m.jpg
http://static.bbc.co.uk/music/images/promotions/366x206/xf3q.jpg
http://static.bbc.co.uk/homepage/img/expanded.png
http://static.bbc.co.uk/homepage/img/x.gif
http://news.bbcimg.co.uk/media/images/48973000/jpg/_48973705_blairgetty.jpg
http://static.bbc.co.uk/homepage/img/plus.gif
http://static.bbc.co.uk/homepage/img/minus.gif
http://static.bbc.co.uk/frameworks/barlesque/1.4.8/desktop/2.5/img/footer_blocks_grey.gif
http://su.sageanalyst.net//ns?ci=775&di=d006&pg=&ai=hn:www.bbc.co.uk,uri:/home/d/index.shtml,qs:,rf:/,cgr:8,ver:1,eid:0,ump:0,prm:1,hp:0,uc:+m0003q|s000lq|s0003v>5|s0012e|s0003u+m0000z|s000f6|s000f8>3|s000f9>3+m0007k|s000d3|s000d6|s000d5>3|s000cz+m0007m|s000rv|s000s6>3|s0002u^+m0007p|s0002p+m0005n|s0005p+m0007n|s000i8|s000ia>3|s000i4+m0000b|s00005+m0007l|s000j6|s000jb>2|s000jc|s000jd^+m0010k|s0010m+m0007s|s000b4|s000am>2+m000z0|s000za+m0007h|s000vq|s000vr+m0007r|s0007x|s00080+m0007t|s000nd|s000n5>5^,uu: