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

  • Home
  • SEARCH
  • 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 8605819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:55:56+00:00 2026-06-12T02:55:56+00:00

How do I exactly match multiple instances of img tags? I read up a

  • 0

How do I exactly match multiple instances of img tags? I read up a few tutorials on preg_match yet never really quite understand.

I have this as my base:

<img src="http://example.com/1.png" alt="Example" />

<img class="Class" src="http://example.com/2.jpg" alt="Example 2" />

And I did up a small like regex:

<img (src="|class="Class" src=")http://.+\.(?:jpe?g|png)" alt="

After this, I’m stuck. How do I continue to match all till the end of the both strings?

I found out about the array part on PHP website itself:

preg_match('@^(?:http://)?([^/]+)@i',
    "http://www.php.net/index.html", $matches);
$host = $matches[1];

Using my code, how do I get the image URL, and the alt tag?

Thanks!

  • 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-06-12T02:55:57+00:00Added an answer on June 12, 2026 at 2:55 am

    For the original question, use preg_match_all() function to get all the matches.

    For the second question (“Using my code, how do I get the image URL, and the alt tag?”), basically your regex is correct. However, I would suggest to get the whole <img> tag first, then do another preg_match() to get the href and alt attributes, since their order may vary.

    $html = "<img src='test.jpg' alt='aaaaaaaaaaa!'>  adfa <img src='test2.jpg' alt='aaaaaaaaaaa2'>  ";
    
    $pattern = '/<img\s[^>]*>/';
    $count = preg_match_all($pattern, $html, $matches, PREG_SET_ORDER);
    
    echo "Found: " . $count . "\n";
    if ($count > 0) {
        foreach ($matches as $match) {
            $img = $match[0];
            echo "img: " . $img . "\n";
            if (preg_match("/src=['\"]([^'\"]*)['\"]/", $img, $val)) {  # UPDATE: use () to catch the content of src
                $src = $val[1];      # UPDATE: get the part in ()
            }
            if (preg_match("/alt=['\"]([^'\"]*)['\"]/", $img, $val)) {   # UPDATE
                $alt = $val[1];      # UPDATE
            }
    
            echo "src = " . $src . ", alt = " . $alt . "\n";
        }
    }
    

    UPDATE

    Answer to your comment.
    Sure. Just use a group to catch the part after src=.
    I updated the source above and commented with “UPDATE”.

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

Sidebar

Related Questions

If the itemcount property does not exactly match the number of indexed rows is
I'm trying to match a SEDOL (exactly 7 chars: 6 alpha-numeric chars followed by
Questions exactly like this have been asked before, i've read them all and tried
I'm trying to use Java regexps to match a pattern that spans multiple lines.
this debugging option: Require source files to exactly match the original version This is
how do i exactly match a word like $abc from a string this is
How to exactly match a given string in a sentence. For example if the
Is it possible to make the color scheme of GVim to exactly match the
When I load my web page, I want my background image to exactly match
Exactly as the title says. I've created an @NodeEntity annotated POJO and in it

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.