Jump to content

[SOLVED] img name and ext grabbing


mike42

Recommended Posts

hi

 

i will to grab this image tags and download from source url

 

match string: <div id="divnewsh"><img src="/news/image/imagename.jpeg" alt="blabla" width="300" border="0" class="imgnews"/></div>

 

how can i  only image name and ext. (imagename.jpeg) matching

 

if (preg_match('#<div id="divnewsh"><img src="(.*?)"#', $f, $matches)) {
				    				
$images=$matches[1];
$images=trim($images);
$url = "http://urldomain.com".$images; 
$target  = "./news/".$images; 

$file = fopen($target,"r"); 
while(!feof ($file))
{ 
$content1 = fgets($file, 4096); 
$content .=$content1;
}
fclose($file); 
$file = fopen($target,"w"); 
fwrite($file,$target); 
fclose($file); 		
}								

 

thanks..

Link to comment
https://forums.phpfreaks.com/topic/123705-solved-img-name-and-ext-grabbing/
Share on other sites

Here's what I came up with...

 

$str = '<div id="divnewsh"><img src="/news/image/imagename.jpeg" alt="blabla" width="300" border="0" class="imgnews"/></div>';
preg_match('#([^/]+\.(?:jpeg|jpg|gif))#i', $str, $match);
echo $match[1];

 

Ouput:

imagename.jpeg

 

From an efficiency perspective, I have a feeling it can be faster... but for this single string check, it seems to do the trick.

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.