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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T10:39:23+00:00 2026-05-21T10:39:23+00:00

I’ve been trying my best to avoid coming here and asking this, insisting to

  • 0

I’ve been trying my best to avoid coming here and asking this, insisting to myself that I could solve it on my own. I have done that, but I thought i’d come here anyway to 1) share my solution or 2) get a better solution.

I know there are already a ton of stackoverflow questions on this, most say use the PEAR library and none are about my specific question.

Basically I want to be able to parse the bbcode quote tag, however this quote can have a variable number of attributes or no attributes at all so a simple preg_replace won’t work in the same way as it would for say an underline tag.

There can also be multiple quote tags within one string, here’s an example of how I solved it. Can anyone suggest a better way avoiding the multiple regex expressions and foreach loops?

(it should be noted i’m parsing the strong tag in the example, but I am doing this elsewhere in my code, its the quotes i’m specifically struggling with and asking about here)

$string = "[quote name='Rob' user_id='1' id='1' timestamp='1294120376']
My text here
[/quote]

[quote name='Rob' user_id='1' id='2' timestamp='1302442553']
Lorem ipsum dolor sit amet
[/quote]

Test Comment";

preg_match_all('/\[quote(.*?)](.*?)\[\/quote\]/msi', $string, $matches);

$quotes = array();

foreach($matches[1] as $id => $match)
{
    preg_match_all('/(\w*?)=\'(.*?)\'/msi', $match, $attr_matches);

    array_push($quotes, array(
        'text'          =>  trim($matches[2][$id]),
        'attributes'    =>  array_combine($attr_matches[1], $attr_matches[2])
    ));
}

echo '<pre>'.print_r($quotes,1).'</pre>';

This will output the following:

Array
(
    [0] => Array
        (
            [text] => My text here
            [attributes] => Array
                (
                    [name] => Rob
                    [user_id] => 1
                    [id] => 1
                    [timestamp] => 1294120376
                )

        )

    [1] => Array
        (
            [text] => Lorem ipsum dolor sit amet
            [attributes] => Array
                (
                    [name] => Rob
                    [user_id] => 1
                    [id] => 2
                    [timestamp] => 1302442553
                )

        )

)

Then I simply build the HTML

$bbcode = '';

foreach($quotes as $quote)
{
    $attributes = array();
    foreach($quote['attributes'] as $key => $value)
    {
        switch($key)
        {
            case 'id':
                $attributes[] = '<a href="'.site_url('forums/findpost/'.$value).'">Permalink</a>';
            break;
            case 'name':
                if(isset($quote['attributes']['user_id']))
                {
                    $attributes[] = 'By <a href="'.site_url('user/profile/'.$quote['attributes']['user_id'].'/'.$value).'">'.$value.'</a>';
                }
                else
                {
                    $attributes[] = 'By '.$value;
                }
            break;
            case 'timestamp':
                $attributes[] = 'On '.date('d F Y - H:i A', $value);
            break;
        }
    }

    if(!empty($attributes))
    {
        $bbcode .= '<p class="citation">'.implode(' | ', $attributes).'</p>';
    }


    $bbcode .= '<blockquote>
        '.$quote['text'].'
    </blockquote>';
}

echo $bbcode;

Which will output the following:

<p class="citation">By <a href="http://domain.com/user/profile/1/Rob.html">Rob</a> | <a href="http://domain.com/forums/findpost/1.html">Permalink</a> | On 04 January 2011 - 05:52 AM</p>
<blockquote>
    My text here
</blockquote>

<p class="citation">By <a href="http://domain.com/user/profile/1/Rob.html">Rob</a> | <a href="http://domain.com/forums/findpost/2.html">Permalink</a> | On 10 April 2011 - 14:35 PM</p>
<blockquote>
    Lorem ipsum dolor sit amet
</blockquote>

So this seems like a very long and round about way of doing it but I can’t fathom another method. Anyone…?

  • 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-21T10:39:24+00:00Added an answer on May 21, 2026 at 10:39 am

    I’ve managed to come up with my own more elegant solution that is both less code and will work with nested quotes.

    This will only parse the quotes, the content within and around the quotes will still need to be converted from bbcode, but there are plenty of resources available for that.

    function parse_quote($matches) {
        $bbcode = '';
        preg_match_all('/(\w*?)=\'(.*?)\'/msi', $matches[1], $attr_matches);
        $attributes = array_combine($attr_matches[1], $attr_matches[2]);
        if(!empty($attributes))
        {
            $attribute_strings = array();
            foreach($attributes as $key => $value)
            {
                switch($key)
                {
                    case 'id':
                        $attribute_strings[] = '<a href="http://domain.com/forums/findpost/'.$value.'">Permalink</a>';
                    break;
                    case 'name':
                        if(isset($quote['attributes']['user_id']))
                        {
                            $attribute_strings[] = 'By <a href="http://domain.com/user/profile/'.$attributes['user_id'].'/'.$value.'">'.$value.'</a>';
                        }
                        else
                        {
                            $attribute_strings[] = 'By '.$value;
                        }
                    break;
                    case 'timestamp':
                        $attribute_strings[] = 'On '.date('d F Y - H:i A', $value);
                    break;
                }
            }
    
    
            {
                $citation = '<p class="citation">'.implode(' | ', $attribute_strings).'</p>'."\n";
            }
        }
        else
        {
            $citation = '';
        }
    
        return $citation.'<blockquote>';
    }
    
    $string = "[quote name='Rob' user_id='1' id='1' timestamp='1294120376']
    [quote name='Rob' user_id='1' id='2' timestamp='1302442553']
    [quote name='Rob' user_id='1' id='3' timestamp='1302442553']
    Test at a comment of a third depth
    [/quote]
    Lorem ipsum dolor sit amet
    [/quote]
    This is my comment
    [/quote]
    
    [b]Test Comment[/b]";
    
    $new_string = str_replace('[/quote]', '</blockquote>', $string);
    echo preg_replace_callback('/\[quote(.*?)\]/msi','parse_quote', $new_string);
    

    This should return the following

        <p class="citation">By Rob | <a href="http://domain.comforums/findpost/1">Permalink</a> | On 04 January 2011 - 05:52 AM</p>
    <blockquote>
    <p class="citation">By Rob | <a href="http://domain.comforums/findpost/2">Permalink</a> | On 10 April 2011 - 14:35 PM</p>
    <blockquote>
    <p class="citation">By Rob | <a href="http://domain.comforums/findpost/3">Permalink</a> | On 10 April 2011 - 14:35 PM</p>
    
    <blockquote>
    Test at a comment of a third depth
    </blockquote>
    Lorem ipsum dolor sit amet
    </blockquote>
    This is my comment
    </blockquote>
    
    Test Comment
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.