Code: Select all
[quote="Grim..."]Blah blah[/quote]How's that done then?Grim... wrote:Blah blah
I assume you use regex to pull out the stuff between the
and the stuff in with
between, er, quotes ("<-- that sort of quotes).
Moderator: General Moderators
Code: Select all
[quote="Grim..."]Blah blah[/quote]How's that done then?Grim... wrote:Blah blah
and the stuff in with
between, er, quotes ("<-- that sort of quotes).
Code: Select all
function forumQuotes($text) {
if (stripos($text, "[quote") === FALSE)
{
return ($text);
}
//remove the quote from the text
$boom = explode("[quote", $text);
$bang = explode("[/quote]", $boom[1]);
$quote = $bang[0];
$bang = explode("]", $quote); //bang[0] is the username, bang[1] is the quote text
if (substr($bang[0], 0, 1) != "=")
{
//no name
$quote = $bang[1];
}
else
{
$bang[0] = str_replace("=", "", $bang[0]);
$bang[0] = str_replace("\"", "", $bang[0]);
$quote = $bang[0]." says ".$bang[1];
}
return "<div class=\"quote\">".$quote."</div>";
}Code: Select all
<?php
$str = "[quote="Van Damme"]Jean Claude Van Damme will star with former Miss World Aishwarya Rai in 3rd Asterix and Obelix movie[/quote]";
echo preg_replace("#[quote="(.*?)"](.*?)[/quote]#", "<span class="quote">\\1 wrote:</span><div class="quote-body">\\2</div>", $str);
?>Code: Select all
[quote]This is an unnamed quote[/quote]Code: Select all
[quote="Grim..."]This is a named quote[/quote]Code: Select all
function add_quotes($input)
{
$ret = preg_replace_callback('/\[quote(="([^"]+)")?\](.*?)\[\/quote\]/is', 'parse_bb_quotes', $input);
return $ret;
}
function parse_bb_quotes($matches)
{
$header = !empty($matches[2]) ? $matches[2].' wrote:' : 'Quote:';
$ret = <<<END
<div style="width: 500px;"'>
<div style="font-weight: bold; padding: 2px;">{$header}</div>
<div style="border: 1px solid #CCCCCC; padding: 2px; background: #FAFAFA;">
{$matches[3]}
</div>
</div>
END;
return $ret;
}Code: Select all
$string = <<<EOD
Look what I just found....
[quote="d11wtq"]
I'm a genius!
[/quote]
and this... not sure who said it....
[quote]
No he's not!
[/quote]
Cool
EOD;
echo add_quotes($string);Code: Select all
Look what I just found....
<div style="width: 500px;"'>
<div style="font-weight: bold; padding: 2px;">d11wtq wrote:</div>
<div style="border: 1px solid #CCCCCC; padding: 2px; background: #FAFAFA;">
I'm a genius!
</div>
</div>
and this... not sure who said it....
<div style="width: 500px;"'>
<div style="font-weight: bold; padding: 2px;">Quote:</div>
<div style="border: 1px solid #CCCCCC; padding: 2px; background: #FAFAFA;">
No he's not!
</div>
</div>
Cool :)Code: Select all
$string = 'Look what I just found....
[quote="d11wtq"]
Im a genius!
[quote="Jcart"] nah uh!!! [/quote]
[/quote]
and this... not sure who said it....
[quote]
No hes not!
[/quote]
Cool ';Yes.... to do nested quotes you really need to tokenize and stack and parseJcart wrote:Try running
This smells stack based parserCode: Select all
$string = 'Look what I just found.... [quote="d11wtq"] Im a genius! [quote="Jcart"] nah uh!!! [/quote] [/quote] and this... not sure who said it.... [quote] No hes not! [/quote] Cool ';
Code: Select all
<?php
error_reporting(E_ALL);
class BBCode
{
private $source;
private $output;
private $tags = array(
'quote' => '\[quote(?:="([^"]+)")?\]'
);
private $tokens = array();
function __construct($input)
{
$this->setSource($input);
}
private function setSource($source)
{
$this->source = $source;
}
//Recursive
private function tokenize($tag, $text=false, $stack=array(), $recursing=false)
{
if (!$text && !$recursing) $text = $this->source;
elseif (!$text && $recursing) return $stack; //Nothing left to do
$re = '@'.$this->tags[$tag].'|\[/'.$tag.'\]@is'; //Opening tag or closing tag
if (preg_match($re, $text, $matches, PREG_OFFSET_CAPTURE))
{
$chunks = $matches[0];
$offset = 0;
if ($chunks[1] > 0)
{
$offset = $chunks[1]; //Preg offset (substr)
$plain_text = substr($text, 0, $offset);
$stack[] = $plain_text; //Text before the tag
}
$stack[] = $chunks[0]; //The actual tag
$text = substr($text, (strlen($chunks[0])+$offset)); //Drop chunk off the string since already processed
return $this->tokenize($tag, $text, $stack, 1); //Recurse
}
else
{
$stack[] = $text; //No match, nothing left to do
return $stack;
}
}
public function parseQuotes($source='')
{
if (empty($source)) $source = $this->source;
$this->tokens = $this->tokenize('quote', $source);
$opened = 0;
$ret = '';
foreach ($this->tokens as $tok)
{
if (preg_match('@'.$this->tags['quote'].'@is', $tok, $matches))
{
$info = 'Quote';
if (!empty($matches[1])) $info = $matches[1].' wrote';
$ret .= '<div style="border: 1px solid #AAAAAA; padding: 4px; margin: 4px;">
<div style="font-weight: bold;">'.$info.':</div>';
}
elseif (preg_match('@\[/quote\]@is', $tok, $matches))
{
$ret .= '</div>';
}
else
{
$ret .= $tok;
}
}
$this->output = $ret;
return $this->output;
}
public function dumpTokens()
{
echo '<pre>'.print_r($this->tokens, 1).'</pre>';
}
private function getOutput()
{
return $this->output;
}
public function fetchResult()
{
return $this->getOutput();
}
}
$text = <<<EOD
Just a test and a [quote]here[/quote]
This should be nested
[quote="foo"]
Look at this [quote]here[/quote] ok?
[/quote]
and end here
EOD;
$bb = new BBCode($text);
$bb->parseQuotes();
$bb->dumpTokens();
echo $bb->fetchResult();
?>