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

  • Home
  • SEARCH
  • 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 9147985
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:08:43+00:00 2026-06-17T11:08:43+00:00

I need to replace BBCode quotes from a phpBB3 forum using PHP. Quoted posts

  • 0

I need to replace BBCode quotes from a phpBB3 forum using PHP. Quoted posts look like this:

[quote="John Doe":2sxn61wz][quote="Bob":2sxn61wz]Some text from Bob[/quote:2sxn61wz]Some text from John Doe[/quote:2sxn61wz]Some more text

I would like to parse this string and end up with an array like:

Array (

    [0] => Array (

        [0] => 'John Doe' 
        [1] => 'Some text from John Doe'
    )

    [1] => Array (

        [0] => 'Bob' 
        [1] => 'Some text from Bob'
    )
)

What would be the best approach to recursively find these quote blocks and their content? Thanks in advance for any help on that!

As suggested in the comments:

$str = '[quote="John Doe":2sxn61wz][quote="Bob":2sxn61wz]Some text from Bob[/quote:2sxn61wz]Some text from John Doe[/quote:2sxn61wz]Some more text';
$uid = '2sxn61wz';

print_r(quoteParser($str, $uid));

function quoteParser($str, $uid) {

    $pattern = "#\[quote(?:="(.*?)")?:$uid\]((?!\[quote(?:=".*?")?:$uid\]).)?#ise";

    echo "Unparsed string: " . $str . "<br /><br />";
    echo "Pattern: " . $pattern . "<br /><br />";

    preg_match_all($pattern, $str, $matches);

    return $matches;
}

Output:

Array ( [0] => Array ( [0] => [quote="John Doe":2sxn61wz] [1] => [quote="Bob":2sxn61wz]S ) [1] => Array ( [0] => John Doe [1] => Bob ) [2] => Array ( [0] => [1] => S ) ) 

That’s quite what I need but I don’t get the quoted text. Only the user names. Any help? Thanks so far.

  • 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-17T11:08:45+00:00Added an answer on June 17, 2026 at 11:08 am

    Apparently what you’re wanting is to turn your BBCode output into HTML. You can check the bbcode_create() function if you like. Otherwise a different recursion -to format your text- is a better option, I believe.

    <style type="text/css">
    
        body {
            font-size:.9em;
            font-family:Arial, Helvetica, sans-serif;
        }
    
        .post {
            margin:1em;
            padding:1em;
            font-size:1em;
            border:1px solid #999;
        }
        .post > .text {margin:1em .4em}
        .post > div:first-child {margin-top:0}
        .post > div:last-child {margin-bottom:0}
    
        .post > .quote {border-color:#CCC}
        .quote {
            margin:1em 2em;
            background-color:#F9F9F9;
            border:1px solid #AAA;
            font-style:italic;
        }
        .quote .text {margin:.7em 1em}
        .quote .author {
            color:#039;
            font-size:.8em;
            background-color:#E0E0E0;
            padding:.5em .8em;
            margin:.5em;
        }
        .author a {
            font-weight:bold;
            text-decoration:none;
            color:inherit;
        }
        .author a:hover {text-decoration:underline;}
    
    </style>
    

    function str2html($str,$className='post') {
        $patPost =
        '~(
            \[quote=&quot;(?:(?!&quot;).)+&quot;:(\w+)(?:\])
                (?:
                    (?(?=\[quote=&quot;(?:(?!&quot;).+)&quot;:\w+\])
                        (?R)
                        |
                        (?(?!\[/quote:\2\]).)
                    )*
                )
            \[/quote:\2\]
        )~xis';
        // note that these 2 are not identical
        $patQuote =
        '~
            (\[quote=&quot;((?:(?!&quot;).)+)&quot;:(\w+)(?:\]))
                (
                    (?(?=\[quote=&quot;(?:(?!&quot;).+)&quot;:\w+\])
                        (?R)
                        |
                        (?(?!\[/quote:\3\]).)
                    )*
                )
            (\[/quote:\3\])
        ~xis';
    
        $arr = preg_split($patPost,$str,-1,PREG_SPLIT_DELIM_CAPTURE);
        foreach ($arr as &$value) $value = trim($value);
        unset($value);
        //echo '<h2>split post</h2>'; print_r($arr);
    
        $res = empty($className) ? '' : "<div class=\"{$className}\">";
        for ($i = 0; $i < count($arr); $i += 3) {
            if (!empty($arr[$i]))
                $res .= '<div class="text">' . $arr[$i] . '</div>';
            if (!empty($arr[$i+1]))
                $res .= preg_replace_callback($patQuote,'replaceQuote',$arr[$i+1]);
        }
        $res .= empty($className) ? '' : '</div>';
        return $res;
    }
    function replaceQuote($m) {
        //echo '<h2>replacing quotes</h2>'; print_r($m);
        $res = '<div class="quote">';
        $res .= '<div class="author">';
        $res .= '<a href="">' . $m[2] . '</a> wrote:';
        $res .= '</div>';
        $res .= str2html($m[4],'');
        $res .= '</div>';
        return $res;
    }
    

    You can call this by echo str2html('some string here'). You get the idea.

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

Sidebar

Related Questions

need to replace <wiki>this page</wiki> to <a href='wiki/this_page'>this page</a> using callback function: text =
I need to replace all & with with &amp; in a string like this:
I need to replace some string in a PHP code to make it look
I need replace multiline string in file, like this: startString bla bla bla ...
I need to replace some value in given text using c# preferably using regex
I need to replace the Bind event from JQuery with some event from JavaScript.
I need to replace a word AAAA in a file using dictionary: dictionary.txt AXF1
I need to replace link index.php?a=2 ,but if it's have & after a=2 then
I need to do this : when a user insert a BBCode tag, with
I need help finding a viable solution to convert bbcode to html, this is

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.