1
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]用換行符例如,它會輸出這樣的:bbcode_parser不會允許換行符
[quote]This is {LINEBREAK}
a test[/quote]
而不是以此爲應該
<div class="quote"><div class="quote-author">Quote</div>This is {LINEBREAK}
a test</div>
任何人都有一個想法?
嘗試添加's'修改到您正則表達式(['PCRE_DOTALL'(http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php))。 – DaveRandom 2012-04-23 18:25:58
你是什麼意思?或者在哪裏? – Qzen 2012-04-23 18:26:47
將表達式字符串改爲''#\ [('。$ tags。')=?(。*?)\](。+?)\ [/ \ 1 \]#s'' - 注意's''在最後 – DaveRandom 2012-04-23 18:27:57