I wrote the following code to extract the first jpg or png image url from an HTML file:
$img="";
$pattern1 = '#<img\s+.+?\s*/?>#ise';
preg_match($pattern1, $content, $match1);
foreach($match1 as $i){
$img=$i;
}
$url="";
$pattern2 = '#http://[a-z\dçñ_(),.%&\-+/\s]+(png|jpe?g)#i';
preg_match($pattern2, $img, $match2);
foreach($match2 as $i){
if (strncmp("http://",$i,7)==0 && strlen($i)>7) {
$url=$i;
}
}
As you can see I'm a newbie at regular expressions. Is there a simpler way to do the same?
Thanks in advance