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 6039437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:22:06+00:00 2026-05-23T06:22:06+00:00

I am using this PHP function to grab all <img> tags within any given

  • 0

I am using this PHP function to grab all <img> tags within any given HTML.

function extract_images($content)
{
    $img    = strip_tags(html_entity_decode($content),'<img>');
    $regex  = '~src="[^"]*"~';    

    preg_match_all($regex, $img, $all_images);

    return $all_images;
}

This works and returns all images (gif, png, jpg, etc).

Anyone know how to change the regex…

~src="[^"]*"~

in order to only get files with JPG or JPEG extension?

Thanks a bunch.

  • 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-23T06:22:06+00:00Added an answer on May 23, 2026 at 6:22 am

    Sooner or later the Regex Enforcement Agency will show up. It might as well be me 🙂

    The proper way to do this is with a proper HTML DOM parser. Here’s a DOMDocument solution. The usefulness of this is in that it’s more robust than parsing the HTML by regex, and also gives you the ability to access or modify other HTML attributes on your <img> nodes at the same time.

    $dom = new DOMDocument();
    $dom->loadHTML($content);
    
    // To hold all your links...
    $links = array();
    
    // Get all images
    $imgs = $dom->getElementsByTagName("img");
    foreach($imgs as $img) {
      // Check the src attr of each img
      $src = "";
      $src = $img->getAttribute("src");
      if (preg_match("/\.jp[e]?g$/i", $src) {
    
        // Add it onto your $links array.
        $links[] = $src;
    }
    

    See other answers for the simple regex solution, or adapt from the regex inside my foreach loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using MySQL DATE_FORMAT function to grab the date in the format i
i am using this widget to get my users to create accounts: <iframe src=http://www.facebook.com/plugins/registration.php?
Hi I'm trying to grab all pictures from a specific album (always the same
I am using zend framework to grab user's email inbox. I got token from
I have a PHP page that will collect mp3 links from downloads.nl. The result
i am using persistence.js in a project and i am having some issues with
I am probably making this more complicated than it need be and hope you
Little bit of a beginner here, working on a personal project to scrape my
So I decided to give codeigniter a chance and convert my existing site which

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.