Jump to content

match all images with an href


scarhand

Recommended Posts

im trying to match all images with an href (any image that is linked to somewhere)

 

also, this needs to work for multi-lines

 

heres my regex so far (which only works 50/50):

 

preg_match_all('|<(.*)a(.*)href="(.*)"(.*)>(.*)<img(.*)src="(.*)"(.*)>(.*)<\/a>|i', $gfcontent, $gfmatches);

 

im using $gfmatches[7][num] for the image src

 

the problem is, sometimes the src comes out as 'image.jpg width="10" height="20' instead of just 'image.jpg'

 

yes, i suck at regex, please help!

Link to comment
Share on other sites

thanks

 

ive come up wth this:

 

$regex = "|<.?a.?href.?=.?[\"|\'](.*)[\"|\'].?>.?<.?img.?src.?=.?[\"|\'](.*)[\"|\'].?>.?<.?\/a.?>|i"; 

 

so you can see more of what i am trying to accomplish

 

i am trying to extra the img SRC and HREF from a site that has linked images

 

could you give me a big more help with this regex?

Link to comment
Share on other sites

Okay let's back up a minute.  Post several examples of what the actual string may look like.  The way I understand it is, you have for examples:

 

 

<a href='url'><img src='image'></a>

<a href='url'><img src='image' width='1' height='1'></a>

 

And so you are saying that you want to extract 'url' and 'image' right?

Link to comment
Share on other sites

examples:

 

#1 - <a href="normal/8.jpg" title="image title"><img src="thumbs/8.jpg" border="0" align="middle" alt="image" style="border: 2 solid #000000" width="130" height="160"></a>

 

#2 - <a href="/pics_content/7591/05.jpg"><img src="/pics_thumbs/7591/05_tn.jpg" width="140" height="140" border="2" alt="image" title="image"></a>

 

so from #1 i want 'normal/8.jpg' and 'thumbs/8.jpg'

 

from #2 i want '/pics_content/7591/05.jpg' and '/pics_thumbs/7591/05_tn.jpg'

 

and i need to get these with the same regex. as you can see the formatting can change but i need to get the 'url' and 'image'

Link to comment
Share on other sites

<?php

/*#1 - <a href="normal/8.jpg" title="image title"><img src="thumbs/8.jpg" border="0" align="middle" alt="image" style="border: 2 solid #000000" width="130" height="160"></a>

 

#2 - <a href="/pics_content/7591/05.jpg"><img src="/pics_thumbs/7591/05_tn.jpg" width="140" height="140" border="2" alt="image" title="image"></a>

 

so from #1 i want 'normal/8.jpg' and 'thumbs/8.jpg'

 

from #2 i want '/pics_content/7591/05.jpg' and '/pics_thumbs/7591/05_tn.jpg'*/

$string = '<a href="normal/8.jpg" title="image title"><img src="thumbs/8.jpg" border="0" align="middle" alt="image" style="border: 2 solid #000000" width="130" height="160"></a>';

$string .= '<a href="/pics_content/7591/05.jpg"><img src="/pics_thumbs/7591/05_tn.jpg" width="140" height="140" border="2" alt="image" title="image"></a>';

$strings = split("</a>", $string);

array_pop($strings);// the last one is not needed.

foreach ($strings as $string) {

    preg_match_all('/href="([^"]+).*src="([^"]+)/i', $string, $matches[]);

}

print_r($matches);

 

die();

?>

 

Should work, may not be the exact way you want it, but I am not sure if it can be done without splitting the strings into an array...

Link to comment
Share on other sites

<?php
/*#1 - <a href="normal/8.jpg" title="image title"><img src="thumbs/8.jpg" border="0" align="middle" alt="image" style="border: 2 solid #000000" width="130" height="160"></a>

#2 - <a href="/pics_content/7591/05.jpg"><img src="/pics_thumbs/7591/05_tn.jpg" width="140" height="140" border="2" alt="image" title="image"></a>

so from #1 i want 'normal/8.jpg' and 'thumbs/8.jpg'

from #2 i want '/pics_content/7591/05.jpg' and '/pics_thumbs/7591/05_tn.jpg'*/
$string = '<a href="normal/8.jpg" title="image title"><img src="thumbs/8.jpg" border="0" align="middle" alt="image" style="border: 2 solid #000000" width="130" height="160"></a>';
$string .= '<a href="/pics_content/7591/05.jpg"><img src="/pics_thumbs/7591/05_tn.jpg" width="140" height="140" border="2" alt="image" title="image"></a>';
$strings = split("</a>", $string);
array_pop($strings);// the last one is not needed.
foreach ($strings as $string) {
    preg_match_all('/href="([^"]+).*src="([^"]+)/i', $string, $matches[]);
}
print_r($matches);

die();
?>

 

Should work, may not be the exact way you want it, but I am not sure if it can be done without splitting the strings into an array...

 

Wow I cannot believe I forgot the code tags, now I feel retarded.

Link to comment
Share on other sites

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.