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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T21:48:07+00:00 2026-06-03T21:48:07+00:00

Note: I’m using WordPress, but I don’t believe it’s relevant to the answer, so

  • 0

Note: I’m using WordPress, but I don’t believe it’s relevant to the answer, so I’ve asked it on SO. If I’m wrong, please tell me/move the question.

Okay, I’m loading up blocks of rich content (via WordPress) which frequently contain many images wrapped in anchor tags. I’d like to step through all of them in order to display them as a tags with their relevant imgs inside.

I’ve already found this handy bit of regex-powered code which gets me the images perfectly well:

            // Get the all post content in a variable
            $posttext = $post->post_content;
            //$posttext1 = get_cleaned_excerpt();

            // We will search for the src="" in the post content
            $regular_expression = '~src="[^"]*"~';
            $regular_expression1 = '~<img [^\>]*\ />~';

            // WE will grab all the images from the post in an array $allpics using preg_match_all
            preg_match_all( $regular_expression, $posttext, $allpics );

            // Count the number of images found.
            $NumberOfPics = count($allpics[0]);

            // This time we replace/remove the images from the content
             $only_post_text = preg_replace( $regular_expression1, '' , $posttext1);
            /*Only text will be printed*/

            // Check to see if we have at least 1 image
            if ( $NumberOfPics > 0 )
            {

            $this_post_id = get_the_ID();


            for ( $i=0; $i < $NumberOfPics ; $i++ )
            {           $str1=$allpics[0][$i];
            $str1=trim($str1);
            $len=strlen($str1);
            $imgpath=substr_replace(substr($str1,5,$len),"",-1);



            $theImageSrc = $imgpath;
            global $blog_id;
            if (isset($blog_id) && $blog_id > 0) {
                $imageParts = explode('/files/', $theImageSrc);
                if (isset($imageParts[1])) {
                    $theImageSrc = '/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
                }
    }

            ?>

            <img class="alignleft" src='<?php echo get_bloginfo('template_directory').'/timthumb.php?src=' . $theImageSrc  . '&h=150&w=150'; ?>' height="150" width="150" alt=""/>

I’d really like to wrap that bottom img with the relevant parent a. Any help here would be greatly appreciated.

An example of the content to be searched might be:

    <h5>
    <a href="http://www.example.com/imagefoo.jpg">
        <img class="size-thumbnail wp-image-4091 alignleft" src="http://www.example.com/imagefoo-150x150.jpg" alt="" width="150" height="150" />
    </a>
</h5>
<h5>
    <a href="http://www.example.com/Image-Bar.jpg">
        <img class="wp-image-4087 alignleft" title="Image - Bar" src="http://www.example.com/Image-Bar-150x150.jpg" alt="" width="150" height="150" />
    </a>
</h5>
<h5>
    <a href="http://www.example.com/Image-Alphe.jpg">
        <img class="wp-image-4090 alignleft" title="Image-Alpha" src="http://www.example.com/Image-Alpha-150x150.jpg" alt="" width="150" height="150" />
    </a>
</h5>
    <a href="http://www.example.com/EXAMPLE-image-150.jpg"><img class="size-thumbnail wp-image-4088 alignleft" title="EXAMPLE-image-150" src="http://www.example.com/EXAMPLE-image-150-150x150.jpg" alt="" width="150" height="150" /></a>
<h5>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</h5>

<a href="http://www.example.com/insanely-long-permalink-created-as-if-by-a-madman-who-knows-no-bounds-of-shame/" rel="attachment wp-att-2780">
    <img class="alignright size-thumbnail wp-image-2780" title="Exhibition Title: Image Name by Artist Person" src="http://www.example.com/wp-content/uploads/2011/12/ExtraordinaryImage-150x150.jpg" alt="Example UK | Exhibition: Image by Artist Person" width="150" height="150" />
</a>
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

EDIT: Here’s the working code based on my needs. It uses XPath, based on cHao’s answer below. (For what it’s worth, I found Tizag’s webpage very useful as an XPath primer, alongside this EarthInfo page.):

            // Get the all post content in a variable
            $posttext = $post->post_content;

            $document = DOMDocument::loadHTML($posttext);
            $xpath = new DOMXPath($document);
             $i = 0;
            # for each link that has an image inside it, set its href equal to
            # the image's src.
            foreach ($xpath->query('//a/img/..') as $link) :


                $img = $link->getElementsByTagName('img')->item(0);
                $link_src = $link->getAttribute('href');
                $link_title = $link->getAttribute('title');
                $img_src = $img->getAttribute('src');


                $theImageSrc = $img_src;
                global $blog_id;
                if (isset($blog_id) && $blog_id > 0) {
                    $imageParts = explode('/files/', $theImageSrc);
                    if (isset($imageParts[1])) {
                        $theImageSrc = '/blogs.dir/' . $blog_id . '/files/' . $imageParts[1];
                    }
                }

                ?>

                <a href="<?php echo $link_src; ?>" rel="lightbox[<?php echo $this_post_id; ?>]" title="<?php if ($link_title) {
                    echo $link_title;
                } else { the_title(); } ?>" class="cboxElement">
                <img class="alignleft" src='<?php echo get_bloginfo('template_directory').'/timthumb.php?src=' . $theImageSrc  . '&h=150&w=150'; ?>' height="150" width="150" alt=""/>
            </a>

            <?php

            endforeach;

            ?>
  • 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-03T21:48:09+00:00Added an answer on June 3, 2026 at 9:48 pm

    You’d be better off not trying to use regular expressions for finding the images. They suck at parsing HTML.

    Instead, check out the DOMDocument and DOMXPath classes.

    $document = DOMDocument::loadHTML($posttext);
    $xpath = new DOMXPath($document);
    
    # for each link that has an image inside it, set its href equal to
    # the image's src.
    foreach ($xpath->query('//a[/img]') as $link) {
        $img = $link->getElementsByTagName('img')->item(0);
        $src = $img->getAttribute('src');
    
        # do your mangling of $src here, resulting in $href.
        # for example...
        $href = preg_replace('/-\d+x\d+(?=\.[^.]*$)/', '', $src);
    
        $link->setAttribute('href', $href);
    }
    
    $fixed_html = $document->saveHTML();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Note: Originally this question was asked for PostgreSQL, however, the answer applies to almost
note: I'm working on wordpress but this isn't a wordpress question. I have a
Note: This might seem like a Super User question at first, but please read
Note: To answer this question, you shouldn't have to know anything about Selenium or
NOTE: XMLIgnore is NOT the answer! OK, so following on from my question on
Note The question below was asked in 2008 about some code from 2003. As
Note This is not a REBOL-specific question. You can answer it in any language.
note: question title is change as discussed in this meta Q&A I'm using the
Note: This is an FAQ, asked specifically so I can answer it myself, as
Note: I already saw this and it doesn't answer the question. I have a

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.