Skip to content

Instantly share code, notes, and snippets.

@lossendae
Created May 4, 2011 21:38
Show Gist options
  • Save lossendae/956092 to your computer and use it in GitHub Desktop.
Save lossendae/956092 to your computer and use it in GitHub Desktop.
nested quote parser example
<?php
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment