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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:41:52+00:00 2026-05-31T08:41:52+00:00

I can’t seem to figure out the proper regular expression for extracting just specific

  • 0

I can’t seem to figure out the proper regular expression for extracting just specific numbers from a string. I have an HTML string that has various img tags in it. There are a bunch of img tags in the HTML that I want to extract a portion of the value from. They follow this format:

<img src="http://domain.com/images/59.jpg" class="something" />
<img src="http://domain.com/images/549.jpg" class="something" />
<img src="http://domain.com/images/1249.jpg" class="something" />
<img src="http://domain.com/images/6.jpg" class="something" />

So, varying lengths of numbers before what ‘usually’ is a .jpg (it may be a .gif, .png, or something else too). I want to only extract the number from that string.

The 2nd part of this is that I want to use that number to look up an entry in a database and grab the alt/title tag for that specific id of image. Lastly, I want to add that returned database value into the string and throw it back into the HTML string.

Any thoughts on how to proceed with it would be great…

Thus far, I’ve tried:

$pattern = '/img src="http://domain.com/images/[0-9]+\/.jpg';
preg_match_all($pattern, $body, $matches);
var_dump($matches);
  • 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-31T08:41:54+00:00Added an answer on May 31, 2026 at 8:41 am

    I think this is the best approach:

    1. Use an HTML parser to extract the image tags
    2. Use a regular expression (or perhaps string manipulation) to extract the ID
    3. Query for the data
    4. Use the HTML parser to insert the returned data

    Here is an example. There are improvements I can think of, such as using string manipulation instead of a regex.

    $html = '<img src="http://domain.com/images/59.jpg" class="something" />
    <img src="http://domain.com/images/549.jpg" class="something" />
    <img src="http://domain.com/images/1249.jpg" class="something" />
    <img src="http://domain.com/images/6.jpg" class="something" />';
    $doc = new DOMDocument;
    $doc->loadHtml( $html);
    
    foreach( $doc->getElementsByTagName('img') as $img)
    {
        $src = $img->getAttribute('src');
        preg_match( '#/images/([0-9]+)\.#i', $src, $matches);
        $id = $matches[1];
        echo 'Fetching info for image ID ' . $id . "\n";
    
        // Query stuff here
        $result = 'Got this from the DB';
    
        $img->setAttribute( 'title', $result);
        $img->setAttribute( 'alt', $result);
    }
    
    $newHTML = $doc->saveHtml();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can't figure out how to do this in a pretty way : I have
Does anyone know how can I replace this 2 symbol below from the string
Can someone please tell me how to show all privileges/rules from a specific user
Can you cast a List<int> to List<string> somehow? I know I could loop through
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
Can anybody tell what is the best(easy) way to sort the following string 'index'
Can't work out a way to make an array of buttons in android. This
Can you have submenus with the top level set to checkable in WPF? I

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.