Jump to content

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/145301-solved-preg_match-more-than-1-occurence/
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.