Extracting links using preg_match_all

Go To StackoverFlow.com

0

I want extract the path of images from a html page using PHP-preg_match_all(), the pattern is as follows

 <img width="148" height="110" src="https://link1">
 <img width="104" height="129" src="https://link2">
 <img width="150" height="129" src="https://linkn">

I want to all the image path in an array.

2012-04-04 04:40
by Alfred Francis


0

Try:

preg_match_all("/<img .*?(?=src)src=\"([^\"]+)\"/si", $html, $m); 
print_r($m);

OR

preg_match_all("/<img .* src=\"([^']*?)\">/", $html, $m);
echo "<pre>";
print_r($m[1]);
2012-04-04 04:43
by Sudhir Bastakoti
It outputs some html content before the array - Alfred Francis 2012-04-04 04:51
you could use the second on - Sudhir Bastakoti 2012-04-04 07:35


-1

You can use simple regex to do that,

Use:

preg_match_all('/<img .*?(?=src)src=\"([^\"]+)\"/si', $imglink, $result, PREG_PATTERN_ORDER);

$imglink is each of your image link

and

$result is the result in array.

with looping:

<?php 
$subject  = array (
     '<img width="148" height="110" src="https://link1">'
    , '<img width="104" height="129" src="https://link2">'
    , '<img width="150" height="129" src="https://linkn">'
);

foreach ($subject as $imglink) {
preg_match_all('/<img .*?(?=src)src=\"([^\"]+)\"/si', $imglink, $result, PREG_PATTERN_ORDER);
$link[] = $result[1];
}

echo "<pre>";
print_r ($link);

 ?>
2012-04-04 05:19
by Lingga Permadi


-1

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"  xml:lang="tr" lang="tr" dir="ltr">
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-9" />
<style type="text/css">
<!-- 
html,body,div,span,h1,h2,h3,p,hr,br,img,form,input,ul,li,a {
 margin:0;
 padding:0;
 border:0;
}
ul li {list-style:none;}
body {
 font-family:Helvetica, Arial, Tahoma, sans-serif;
 font-size:13px;
 color:#444;
 line-height:1.5em;
} 
#kapsayici {
background:#fff;
margin:10px auto;
width:960px;
border:0px solid #dfdfdf;
min-height: 700px;
}
-->
</style>
</head>
<body>
<div id="kapsayici">
<?php
$url = "http://www.mynet.com";
$icerik = file_get_contents($url);

$resimler = array();
preg_match_all('/(img|src)=("|\')[^"\'>]+/i', $icerik, $veriler);
$dizi=preg_replace('/(img|src)("|\'|="|=\')(.*)/i',"$3",$veriler[0]);

foreach($dizi as $row) {
    $bilgi = pathinfo($row);
    if (isset($bilgi['extension'])) {
        $bilgi['extension'] = strtolower($bilgi['extension']);
        if (($bilgi['extension'] == 'jpg') || ($bilgi['extension'] == 'jpeg') || ($bilgi['extension'] == 'gif') || ($bilgi['extension'] == 'png')) array_push($resimler, $row);
    }
}
$resimler=array_unique($resimler);
echo "<ul>\n";
if (count($resimler)) {
    $i_count=0;
    foreach($resimler as $resim) {
        $i_count++;
        echo "<li><img src=\"{$resim}\" /></li>\n";
    }

}
echo "</ul>\n";
?>
</div>
</body>
</html>
2012-09-22 09:54
by merakli
Please add some explanation for better understanding - akjoshi 2013-01-18 11:33
Ads