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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:08:55+00:00 2026-05-22T19:08:55+00:00

Switching the code of the debate forum on my website, I am going to

  • 0

Switching the code of the debate forum on my website, I am going to change the way quotes are stored in the database. Now I need to come up with a regex to rearrange already submitted posts in my database.

Following is an example of how my current debate post are stored in the database (with quotes in quotes).. Note: I have indented it for the sake of illustration:

Just citing a post
[quote]Text of quote #3
       [quote]Text of quote #2
              [quote]Text of quote #1
                     [name]User 1[/name]
              [/quote]
              [name]User 2[/name]
       [/quote]
       [name]User 3[/name]
[/quote]

What I would like now, is that the former will be rearranged to look like this:

Just citing a post
[quote:User 3]
      Text of quote #3
      [quote:User 2]
           Text of quote #2
           [quote:User 1]
                 Text of quote #1
           [/quote]  
      [/quote]   
[/quote]

Can any of you point me in the direction of how this can be done with regex? I am using PHP.

Thanks in advance, I appreciate all your help 🙂

Fischer

  • 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-22T19:08:56+00:00Added an answer on May 22, 2026 at 7:08 pm

    This will do it:

    $input = "Just citing a post
    [quote]Text of quote #3
           [quote]Text of quote #2
                  [quote]Text of quote #1
                         [name]User 1[/name]
                  [/quote]
                  [name]User 2[/name]
           [/quote]
           [name]User 3[/name]
    [/quote]";
    
    function fix_quotes($string) {
        $regexp = '`(\s*)\[quote\]((?:[^\[]|\[(?!quote\]))*?)\[name\](.*?)\[\/name\]\s*\[\/quote\]`';
        while (preg_match($regexp, $string)) {
            $string = preg_replace_callback($regexp, function($match) {
                return $match[1] . '[quote:' . $match[3] . ']' . trim(fix_quotes($match[2])) . $match[1] . '[/quote]';
            }, $string);
        }
        return $string;
    }
    
    echo fix_quotes($input);
    

    Results in:

    Just citing a post
    [quote:User 3]Text of quote #3
           [quote:User 2]Text of quote #2
                  [quote:User 1]Text of quote #1
                  [/quote]
           [/quote]
    [/quote]
    

    Edit: haven’t seen that joelhardi already posted similar solution, and his looks a bit cleaner so I’d stick with his solution 🙂

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

Sidebar

Related Questions

I'm working on a website and constantly switching from development mode (where the code
Possible Duplicate: jQuery 1.7 - Turning live() into on() Just switching my code from
Possible Duplicate: Switching to assembly in gdb I am debugging some code in gdb.
Possible Duplicate: Django switching, for a block of code, switch the language so translations
I am a vb.net programmer switching to C#. I have the following code for
I've started switching over a project from hand-written JDBC ORM code to Ebeans. So
Is there any VS2008 shortcut for switching between View designer / View code? I
I often listen to music while I code. Switching songs while in the middle
I'm switching code generators for my business objects. I was using SQL Metal, but
I´m having a problem switching css when Javascript is disabled. I have this code:

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.