简易 UBB 转化为 HTML的函数 zpz_ubb2html

这里法一个 简易 UBB 转化为 HTML的函数 zpz_ubb2html,没有支持所有的 UBB标签,而且不支持嵌套,有时间会做一个基于 DFA 的高级 ubb 解析器。

function zpz_ubb2html($sText){
	$aUBBReplacement = array(
		"`\n`" => '<br>',
		"`\\t`s" => '  ',
		'`\[hr\]`is' => '<hr>',
		'`\[separator\]`is' => '<br>',
		'`\[quote\](.+?)\[/quote\]`is' => '<blockquote>\1</blockquote>',
		'`\[h([1-6])\](.+?)\[/h\1\]`i' => '<h\1>\2</h\1>',
		'`\[(i|u|b|center|sub|sup|pre)\](.+?)\[/\1\]`i`' => '<\1>\2</\1>',
		'`\[url\](.+?)\[/url\]`i' => '<a href="\1" target="_blank">\1</a>',
		'`\[url=(http://.+?)\](.+?)\[/url\]`is' => '<a href="\1" target="_blank">\2</a>',
		'`\[url=(.+?)\](.+?)\[/url\]`is' => '<a href="\1">\2</a>',
		'`\[img\](.+?)\[/img\]`is' => '<img src="\1">',
		'`\[img\s(.+?)\](.+?)\[/img\]`is' => '<img \1 src="\2">',
		'`\[color=(.+?)\](.+?)\[/color\]`is' => '<span style="color:\1">\2</span>',
		'`\[style=(.+?)\](.+?)\[/style\]`is' => '<div class="\1">\2</div>',
		'`\[size=(.+?)\](.+?)\[/size\]`is' => '<span style="font-size: \1">\2</span>',
		'`\[emot\](.+?)\[/emot\]`is' => '',
		'`\[email\](.+?)\[/email\]`is', '<a href="mailto:\1">\1</a>',
		'`\[(php|code)\](.+?)\[/\1\]`is' => '<code>\1</code>',
		'`\[sig\](.+?)\[/sig\]`is' => '<div class="signature"><br><br>--------------------------<br>\1<br>--------------------------</div>'
	);
	return preg_replace(array_keys($aUBBReplacement), array_values($aUBBReplacement), trim($sText));
}

Post a Comment

Your email is never shared. Required fields are marked *

*
*