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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T10:23:07+00:00 2026-06-09T10:23:07+00:00

I am doing some regular expressions in php and matching using the preg_match(); I

  • 0

I am doing some regular expressions in php and matching using the preg_match();

I have a text that might look something like this:

 $imy = "...without sophisticated apparatus<div class="caption"><div class="caption-inner">
 <img src="http://dev.mysite.org/Heatmap.png" alt="" title="" class="image-thumbnail" />
 Caption text</div></div>Some more text...
 <img src="http://dev.mysite.org/Heatmap.png" alt="" title="" class="image-thumbnail" />blablah..."

and my goal is to pick out either the “img” tag enclosed in the “div” tags(including the “div” tags) or just the “img” if it is not enclosed in divs. I also in each case want to capture the address contained in the src attribute of the “img” tag.

This is the pattern I use:

$imagepattern = '/<div class="caption-inner[^>]+>.*<img\b[^>]*\bsrc="([^">]*)"[^>]*>.*<\/div>(<\/div>)?|<img\b[^>]*\bsrc="([^">]*)"[^>]*>/Us';

and it works great for “div” enclosed images, but for the divless images I get weird results for the captured subpattern.

I iteratively call preg_match and remove the match from the subject string before resending it to preg_match. My call to preg_match looks like this:

preg_match($imagepattern,$imy,$image,PREG_OFFSET_CAPTURE)

What I get in my image array when matching against a divless image tag looks like this:

$image = [0] => Array
        (
            [0] => <img src="http://dev.molmeth.org/Heatmap.png" alt="" title="" class="image-thumbnail" />
            [1] => 1
        )

    [1] => Array
        (
            [0] =>
            [1] => -1
        )

    [2] => Array
        (
            [0] =>
            [1] => -1
        )

    [3] => Array
        (
            [0] => http://dev.mysite.org/Heatmap.png
            [1] => 11
        )

How can the $image array have the ‘2’ and ‘3’ keys? Don’t I only have one subpattern? Is this somehow because of the ‘or’ condition in the pattern?

  • 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-09T10:23:09+00:00Added an answer on June 9, 2026 at 10:23 am

    in your preg_match expression you have 3 capture groups.

    the whole expression matches because of the or (since you search div included images OR divless images)

    for divless images, only capture group 3 will be filled data and capture groups 1 & 2 will be empty.

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

Sidebar

Related Questions

I have some unicode text that I want to clean up using regular expressions.
My particular scenario involves doing some text transformation using regular expressions within a private
I've been doing some HTML scraping in PHP using regular expressions. This works, but
I am doing some fairly extensive string manipulations using regular expressions in Java. Currently,
I have been doing some testing with the differences between JavaScript and PHP regular
Hi have some forms that I want to use some basic php validation (regular
I have started to feel that using regular expressions decreases code maintainability. There is
I have a problem with regular expressions. I would like to match strings that
While doing some refactoring I've found that I'm quite often using a pair or
I'm working in C# doing some OCR work and have extracted the text 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.