Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 297553
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:39:01+00:00 2026-05-12T06:39:01+00:00

I would like to know how this can be achieved. Assume: That there’s a

  • 0

I would like to know how this can be achieved.

Assume: That there’s a lot of html code containing tables, divs, images, etc.

Problem: How can I get matches of all occurances. More over, to be specific, how can I get the img tag source (src = ?).

example:

<img src="http://example.com/g.jpg" alt="" />

How can I print out http://example.com/g.jpg in this case. I want to assume that there are also other tags in the html code as i mentioned, and possibly more than one image. Would it be possible to have an array of all images sources in html code?

I know this can be achieved way or another with regular expressions, but I can’t get the hang of it.

Any help is greatly appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T06:39:01+00:00Added an answer on May 12, 2026 at 6:39 am

    While regular expressions can be good for a large variety of tasks, I find it usually falls short when parsing HTML DOM. The problem with HTML is that the structure of your document is so variable that it is hard to accurately (and by accurately I mean 100% success rate with no false positive) extract a tag.

    What I recommend you do is use a DOM parser such as SimpleHTML and use it as such:

    function get_first_image($html) {
        require_once('SimpleHTML.class.php')
    
        $post_html = str_get_html($html);
    
        $first_img = $post_html->find('img', 0);
    
        if($first_img !== null) {
            return $first_img->src;
        }
    
        return null;
    }
    

    Some may think this is overkill, but in the end, it will be easier to maintain and also allows for more extensibility. For example, using the DOM parser, I can also get the alt attribute.

    A regular expression could be devised to achieve the same goal but would be limited in such way that it would force the alt attribute to be after the src or the opposite, and to overcome this limitation would add more complexity to the regular expression.

    Also, consider the following. To properly match an <img> tag using regular expressions and to get only the src attribute (captured in group 2), you need the following regular expression:

    <\s*?img\s+[^>]*?\s*src\s*=\s*(["'])((\\?+.)*?)\1[^>]*?>
    

    And then again, the above can fail if:

    • The attribute or tag name is in capital and the i modifier is not used.
    • Quotes are not used around the src attribute.
    • Another attribute then src uses the > character somewhere in their value.
    • Some other reason I have not foreseen.

    So again, simply don’t use regular expressions to parse a dom document.


    EDIT: If you want all the images:

    function get_images($html){
        require_once('SimpleHTML.class.php')
    
        $post_dom = str_get_dom($html);
    
        $img_tags = $post_dom->find('img');
    
        $images = array();
    
        foreach($img_tags as $image) {
            $images[] = $image->src;
        }
    
        return $images;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know if there exist algorithms that solves this issue. It
I would like to know if there are any templates for doing this in
I would like to know this to understand why some games like Mario is
I would like to know whether this is actually possible in Android? When Android
I would like to know how this homepage from Google Docs app is build
I know this is stupid, but how would I do this? I would like
I know this is a weird question to ask, but I would like to
I would like to animate things in 3D space. I know this is possible
I would like to know how to write this inside my hittest if statement:
I would like to know how to add menus similar to this to my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.