Search code examples
phpregexpreg-replacevbulletinquote

How to parse a Quoted Post from vBulletin?


Using PHP, how can I transform:

[QUOTE=Username;1234]This is the text being quoted[/QUOTE]

Into:

<div class="quote"><a href="./linktopost?id=1234>Posted by: Username</a><p>This is the text being quoted.</p></div>

Notice where the Username, 1234 id, and This is the text being quoted text are mapped to.

I know how to do the beginning and end, by simply replacing (e.g. str_replace) any instance of:

[QUOTE= >>> <div class="quote">< href="./linktopost.php?id=

[/QUOTE] >>> </p><div>

but I don't know how to extract the Username, Post ID, and the Text being quoted so that I can tie them all together.

If it helps, from what I can tell above, the username can be extracted between the delimiters [QUOTE= and ;, the post id can be extracted between ; and ], and the text can be extracted between ] and [/QUOTE], but I don't know how to extract them.

Also, what happens if there is a quote within a quote? The solution should also work in those cases as well.

Any ideas?


Solution

  • This may help ;)

    $str = "[QUOTE=Username;1234]This is the text being quoted[/QUOTE]";
    
    if(preg_match('/\[QUOTE=([^;]+);([0-9]+)\]([^\[]+)\[\/QUOTE\]/i', $str, $matches)){
        echo "<br>USERNAME : ".$matches[1];
        echo "<br>POST ID : ".$matches[2];
        echo "<br>MESSAGE : ".$matches[3];
    }