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…?
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.
This should return the following