Skip to content
WJunction - Webmaster Forum

[PHP] Grab all img links from html/webpage

Status
Not open for further replies.

Vick

Banned
Banned
297
2011
72
/*
For ibby
*/

PHP:
<?
	$request_url ='http://www.wjunction.com/';
 
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $request_url);	// The url to get links from
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);	// We want to get the respone
	$result = curl_exec($ch);
 
	$regex='|<img.*?src="(.*?)"|';
	preg_match_all($regex,$result,$parts);
	$links=$parts[1];
	foreach($links as $link){
		echo $link."<br>";
	}
	curl_close($ch);
 ?>

Outputs->
ohmmIA.jpg
 

7 comments

small quick fix for better use...
Call a website from url & hotlinks all files for right click save as...

Code:
  [COLOR=#000000] [COLOR=#0000BB]<?php
    if ($_GET['url']){
     $request_url = $_GET['url']();
    } else {
     echo "No url has been entered";
    }
  
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $request_url);    // The url to get links from 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    // We want to get the respone 
    $result = curl_exec($ch); 
  
    $regex='|<img.*?src="(.*?)"|'; 
    preg_match_all($regex,$result,$parts); 
    $links=$parts[1]; 
    foreach($links as $link){ 
        echo "<a href='$link'>$link</a><br>"; 
    } 
    curl_close($ch); 
 ?>[/COLOR][/COLOR]
 
Last edited:
/*
For ibby
*/

PHP:
<?
	$request_url ='http://www.wjunction.com/';
 
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $request_url);	// The url to get links from
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);	// We want to get the respone
	$result = curl_exec($ch);
 
	$regex='|<img.*?src="(.*?)"|';
	preg_match_all($regex,$result,$parts);
	$links=$parts[1];
	foreach($links as $link){
		echo $link."<br>";
	}
	curl_close($ch);
 ?>

Outputs->
ohmmIA.jpg
THANKS!

Added the tag ;)
[php]<?
$request_url ='http://www.wjunction.com/';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $request_url); // The url to get links from
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // We want to get the respone
$result = curl_exec($ch);
$regex='|<img.*?src="(.*?)"|';
preg_match_all($regex,$result,$parts);
$links=$parts[1];
foreach($links as $link){
echo "[IMG]" . $link . "</br>";
}
curl_close($ch);
?>[/php]




small quick fix for better use...
Call a website from url & hotlinks all files for right click save as...

Code:
  [COLOR=#000000] [COLOR=#0000BB]<?php
    if ($_GET['url']){
     $request_url = $_GET['url']();
    } else {
     echo "No url has been entered";
    }
  
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $request_url);    // The url to get links from 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);    // We want to get the respone 
    $result = curl_exec($ch); 
  
    $regex='|<img.*?src="(.*?)"|'; 
    preg_match_all($regex,$result,$parts); 
    $links=$parts[1]; 
    foreach($links as $link){ 
        echo "<a href='$link'>$link</a><br>"; 
    } 
    curl_close($ch); 
 ?>[/COLOR][/COLOR]

dont know how to work urs...
 
Theres a bug in the image only which you posted

545957b7e8.png


Make it check for actual url. If url doesn't exist and url starts with "/" then add base url of the website. If url doesn't contains "/" and also doesn't starts with "http://" then add the base link + actual path of the page you are grabbing
 
yay, regex!

i prefer to not use "." (any character)

Code:
#<img[^>]?src="([^"]+)"#

local, i dunno really, maybe he wanted to take images off a site, automatically put them in img tags for him to copypasta, then post them wherever.
 
Status
Not open for further replies.

About the author

V
Banned · Joined
297
Messages
72
Reactions
28
Points
Grow your business

Advertise on WJunction

Connect with an established international community of webmasters, hosting providers, developers and online entrepreneurs.

  • Premium & Premium+ Sticky
  • Dedicated section banners
  • Site-wide banner advertising
Explore advertising options
Back
Top Bottom