AdRock Posted February 15, 2009 Share Posted February 15, 2009 I found a function that uses preg match to find the first occurence of an image tag but I may have more that one image tag in the page Is there an alternative to preg_match which can match mulitlple occurences? <?php /** * Searches for the first occurence of an html <img> element in a string * and extracts the src if it finds it. Returns boolean false in case an * <img> element is not found. * @param string $str An HTML string * @return mixed The contents of the src attribute in the * found <img> or boolean false if no <img> * is found */ function str_img_src($html) { if (stripos($html, '<img') !== false) { $imgsrc_regex = '#<\s*img [^\>]*src\s*=\s*(["\'])(.*?)\1#im'; preg_match($imgsrc_regex, $html, $matches); unset($imgsrc_regex); unset($html); if (is_array($matches) && !empty($matches)) { return $matches[2]; } else { return false; } } else { return false; } } $content= '<img src="test.jpg" alt=""><br/> Morbi ac urna. Suspendisse a libero nec nunc bibendum tempor. Integer sed ipsum. Nunc ultricies ornare nunc. Integer pellentesque rutrum massa. In hendrerit mi non nibh. Vivamus tempus. Maecenas id erat. Vivamus purus. Ut malesuada nisl in lorem. Duis ante augue, pharetra non, posuere a, laoreet sed, elit. Phasellus non libero nec neque hendrerit ultricies. Morbi ornare posuere urna. In id dui ut sapien malesuada condimentum. Sed sapien dui, auctor nec, vulputate vitae, feugiat ac, nulla. Ut semper tellus id lorem.<br /> <img src="test.jpg" alt=""> Suspendisse nec orci at leo dictum faucibus. Praesent pellentesque feugiat urna. Nulla vel magna. Proin nisl. Aenean est nisl, dictum id, facilisis at, dignissim id, pede. Duis tellus odio, semper eu, adipiscing non, tincidunt at, orci. Cras scelerisque felis. Donec vulputate diam eget mauris. Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Morbi molestie, arcu quis vulputate iaculis, metus mi congue turpis, tincidunt congue ligula magna et neque. Aenean tristique dapibus elit. Nam neque.<br/> <img src="test.jpg" alt=""> Cras at magna nec lectus ullamcorper pulvinar. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Maecenas faucibus massa quis nulla. Mauris hendrerit magna non turpis. Aenean vitae elit. Ut nisl. Nulla dolor libero, consequat nec, cursus non, porta facilisis, leo. Class aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Vestibulum tempor nisl. Maecenas suscipit imperdiet lacus. Aliquam vitae lorem. Maecenas lacus diam, facilisis sit amet, mollis vel, vehicula et, ante. Duis nunc. Sed posuere tincidunt leo. Aenean eleifend consequat enim. Mauris nec lacus. Aenean in risus in purus pretium vehicula. Sed sed risus sed justo semper porta. '; ?> <html> <head> </head> <body> <?php echo str_img_src($content); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/145301-solved-preg_match-more-than-1-occurence/ Share on other sites More sharing options...
.josh Posted February 15, 2009 Share Posted February 15, 2009 preg_match_all Quote Link to comment https://forums.phpfreaks.com/topic/145301-solved-preg_match-more-than-1-occurence/#findComment-762777 Share on other sites More sharing options...
AdRock Posted February 15, 2009 Author Share Posted February 15, 2009 I tried that and it returns "Array" Do i need to change anything else such as if (is_array($matches) && !empty($matches)) { I would think in_array but i get wrong parameter count Quote Link to comment https://forums.phpfreaks.com/topic/145301-solved-preg_match-more-than-1-occurence/#findComment-762781 Share on other sites More sharing options...
.josh Posted February 15, 2009 Share Posted February 15, 2009 You can do echo "<pre>"; print_r($matches); To see the structure of $matches. Read the manual on how preg_match and preg_match_all works, and what is returned and how it is returned and more importantly, why. Quote Link to comment https://forums.phpfreaks.com/topic/145301-solved-preg_match-more-than-1-occurence/#findComment-762786 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.