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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:00:09+00:00 2026-05-18T10:00:09+00:00

I need to add some tags before and after images on document and numerate

  • 0

I need to add some tags before and after images on document and numerate them at once.
HTML Document code is:

….

<img src="http://img.example.com/img/mage1.jpg" alt="sometile"> <br> 
<img src="http://img.example.com/img/image72.jpg" alt="sometile"> <br> 
<img src="http://img.example.com/img/imagstr.jpg" alt="sometile"> <br> 
<img src="http://img.example.com/img/image.jpg" alt="sometile"> <br> 
<img src="http://img.example.com/img/imgger.gif" alt="sometile"> <br> 
<img src="http://img.example.com/img/somepic.png" alt="sometile"> <br> 

I need in result code like this

<div><a name="#pic1"></a><img src="http://img.example.com/img/mage1.jpg" alt="sometile"></div>
<div><a name="#pic2"></a><img src="http://img.example.com/img/image72.jpg" alt="sometile"> </div> 
<div><a name="#pic3"></a><img src="http://img.example.com/img/imagstr.jpg" alt="sometile"> </div> 
<div><a name="#pic4"></a><img src="http://img.example.com/img/image.jpg" alt="sometile"> </div> 
<div><a name="#pic5"></a><img src="http://img.example.com/img/imgger.gif" alt="sometile"> </div> 
<div><a name="#pic6"></a><img src="http://img.example.com/img/somepic.png" alt="sometile"> </div> 
  • 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-18T10:00:10+00:00Added an answer on May 18, 2026 at 10:00 am

    I’m not a fan of regex+HTML, but here goes (I just cooked up a simple regex—you probably already have one):

    $s = '<img src="http://img.example.com/img/mage1.jpg" alt="sometile"> <br> 
    <img src="http://img.example.com/img/image72.jpg" alt="sometile"> <br> 
    <img src="http://img.example.com/img/imagstr.jpg" alt="sometile"> <br> 
    <img src="http://img.example.com/img/image.jpg" alt="sometile"> <br> 
    <img src="http://img.example.com/img/imgger.gif" alt="sometile"> <br> 
    <img src="http://img.example.com/img/somepic.png" alt="sometile"> <br>';
    
    $i = 0;
    function wrap($s) {
            global $i;
            $i++;
            return sprintf('<div><a name="pic%d">%s</div>', $i, $s);
    }
    
    print preg_replace('#(<img [^>]+?>) <br>#e', "wrap('\\1')", $s);
    

    (Demo)

    The important part is the e modifier in the preg_replace(). See “Example #4 Using the ‘e’ modifier.”


    EDIT
    As pointed out in the comments, $i is certainly not the best name for a global variable. If this is going to be used as a simple “one-off” transformation it may be alright. If not, change the name to something that will not conflict. Or put it as a public static in a class.

    Also, preg_replace_callback() exists. It’s better suited, although I find the passing a function name as a string and evaluating functionName('arg') almost equally ugly 🙂

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

Sidebar

Related Questions

After inserting new html to DOM,I need to add some listeners to it. But
I need to add some lightweight syntactic sugar to JavaScript source code, and process
I have a form and I need to add some data from database before
I have UserControl and I need to add (generate) some tags, basically Input tags.
What I'm trying to do is to add some HTML tags to my Joomla!
I need to add some tags to an existing XML schema and write it
I need to add some accordian style expand/collapse handles to a series of parent
i need to add some text DIV's on each slide of a Nivoslider. I
i need to add some functionality to an asp.net application that allows the positioning
i'm writing an application that need to add some events to a calendar in

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.