Jump to content

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.

 

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.