function bb_parse($string, $tags = 'b|i|u|quote|url|img|youtube|user') {
$replacement = 0;
while (preg_match_all('`\[('.$tags.')=?(.*?)\](.+?)\[/\1\]`', $string, $matches)) foreach ($matches[0] as $key => $match) {
list($tag, $param, $innertext) = array($matches[1][$key], $matches[2][$key], $matches[3][$key]);
switch ($tag) {
case 'b': $replacement = '<strong>'.$innertext.'</strong>'; break;
case 'i': $replacement = '<em>'.$innertext.'</em>'; break;
case 'u': $replacement = '<span style="text-decoration: underline;">'.$innertext.'</span>'; break;
case 'quote': $replacement = '<div class="quote"><div class="quote-author">'.($param ? 'Quote by: '.$param : 'Quote').'</div>'.$innertext.'</div>'; break;
case 'url': $replacement = '<a href="'.($param ? $param : $innertext).'">'.$innertext.'</a>'; break;
case 'user': $replacement = '<a href="'.url('user/'.($param ? $param : $innertext)).'">'.$innertext.'</a>'; break;
case 'img': $replacement = '<img src="'.$innertext.'" style="max-width: 640px;"/>'; break;
case 'youtube':
$videourl = parse_url($innertext);
parse_str($videourl['query'], $videoquery);
if (strpos($videourl['host'], 'youtube.com') !== FALSE) $replacement = '<div><iframe width="755" height="425" src="http://www.youtube.com/embed/'.$videoquery['v'].'" frameborder="0" allowfullscreen></iframe></div>';
break;
}
$string = str_replace($match, $replacement, $string);
}
return $string;
}
例如,如果我将 [quote] 与换行符一起使用,它将输出:
[quote]This is {LINEBREAK}
a test[/quote]
而不是这应该的
<div class="quote"><div class="quote-author">Quote</div>This is {LINEBREAK}
a test</div>
有人有想法吗?