scarhand Posted December 10, 2008 Share Posted December 10, 2008 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! Quote Link to comment https://forums.phpfreaks.com/topic/136369-match-all-images-with-an-href/ Share on other sites More sharing options...
.josh Posted December 10, 2008 Share Posted December 10, 2008 <img[^>]*> for anything inside an <img....> tag. Quote Link to comment https://forums.phpfreaks.com/topic/136369-match-all-images-with-an-href/#findComment-711442 Share on other sites More sharing options...
scarhand Posted December 10, 2008 Author Share Posted December 10, 2008 <img[^>]*> for anything inside an <img....> tag. yes but i just want the SRC Quote Link to comment https://forums.phpfreaks.com/topic/136369-match-all-images-with-an-href/#findComment-711457 Share on other sites More sharing options...
.josh Posted December 10, 2008 Share Posted December 10, 2008 <img.*src='([^']*)'[^>]*> Quote Link to comment https://forums.phpfreaks.com/topic/136369-match-all-images-with-an-href/#findComment-711490 Share on other sites More sharing options...
scarhand Posted December 10, 2008 Author Share Posted December 10, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/136369-match-all-images-with-an-href/#findComment-711515 Share on other sites More sharing options...
.josh Posted December 10, 2008 Share Posted December 10, 2008 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? Quote Link to comment https://forums.phpfreaks.com/topic/136369-match-all-images-with-an-href/#findComment-711530 Share on other sites More sharing options...
scarhand Posted December 10, 2008 Author Share Posted December 10, 2008 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' Quote Link to comment https://forums.phpfreaks.com/topic/136369-match-all-images-with-an-href/#findComment-711536 Share on other sites More sharing options...
premiso Posted December 10, 2008 Share Posted December 10, 2008 <?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... Quote Link to comment https://forums.phpfreaks.com/topic/136369-match-all-images-with-an-href/#findComment-711596 Share on other sites More sharing options...
.josh Posted December 10, 2008 Share Posted December 10, 2008 (?:\<a.*href="([^"]*)"[^>]*>)[.\W]*(?:\<img.*src="([^"]*)"[^>]*>) Quote Link to comment https://forums.phpfreaks.com/topic/136369-match-all-images-with-an-href/#findComment-711761 Share on other sites More sharing options...
premiso Posted December 10, 2008 Share Posted December 10, 2008 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/136369-match-all-images-with-an-href/#findComment-711764 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.