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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T06:11:48+00:00 2026-06-06T06:11:48+00:00

I have the following HTML statement [otsection]Wallpapers[/otsection] WALLPAPERS GO HERE [otsection]Videos[/otsection] VIDEOS GO HERE

  • 0

I have the following HTML statement

[otsection]Wallpapers[/otsection]
WALLPAPERS GO HERE

[otsection]Videos[/otsection]
VIDEOS GO HERE

What I am trying to do is replace the [otsection] tags with an html div. The catch is I want to increment the id of the div from 1->2->3, etc..

So for example, the above statement should be translated to

<div class="otsection" id="1">Wallpapers</div>
WALLPAPERS GO HERE

<div class="otsection" id="2">Videos</div>
VIDEOS GO HERE

As far as I can research, the best way to do this is via a preg_replace_callback to increment the id variable between each replacement. But after 1 hour of working on this, I just cant get it working.

Any assistance with this would be much appreciated!

  • 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-06T06:11:49+00:00Added an answer on June 6, 2026 at 6:11 am

    Use the following:

    $out = preg_replace_callback(
        "(\[otsection\](.*?)\[/otsection\])is",
        function($m) {
            static $id = 0;
            $id++;
            return "<div class=\"otsection\" id=\"ots".$id."\">".$m[1]."</div>";
        },
        $in);
    

    In particular, note that I used a static variable. This variable persists across calls to the function, meaning that it will be incremented every time the function is called, which happens for each match.

    Also, note that I prepended ots to the ID. Element IDs should not start with numbers.


    For PHP before 5.3:

    $out = preg_replace_callback(
        "(\[otsection\](.*?)\[/otsection\])is",
        create_function('$m','
            static $id = 0;
            $id++;
            return "<div class=\"otsection\" id=\"ots".$id."\">".$m[1]."</div>";
        '),
        $in);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have the following MySQL statement to replace the HTML entity for a
Say I have the following html: <div id=myDiv title=My Div>This is me!</div> I want
i have the following html , and i want to hide all lsHeader which
I have the following HTML: <span id=UnitCost5>$3,079.95 to $3,479.95</span> And i want to use
I have the following HTML: <div id=mydiv> </div> I would like to load content
I have the following html code. <html> <body> <div style=max-width:1600px; min-width:900px; > <table> </table>
I have the following statement in document.ready function: if($(sidebar ).html().trim().length == 0) { $(sidebar).append(<p>&nbsp;&nbsp;&nbsp;The
I have the following statement: <%: Html.DropDownList(dropCity, new[] { new SelectlistItem { Text =
I have following HTML <tr id=r1 class=l1> <td><img class=plusminus id=r1c1 src=assets/images/plus.png border=0 />3/10/12</td> <td></td>
I have the following html/css that is causing problems in Firefox 1.5 and 2,

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.