自己写的一个UBB转换的函数_PHP
时间:2021-07-01 10:21:17
帮助过:5人阅读
function ubb2xhtml($ubb)
{
$flash=<<
END;
$match = array
(
'%\[url=([^\s] )\](.*?)\[/url\]%s',
'%\[email=([^\s]) \](.*?)\[/email\]%i',
'%\[img width=(\d ) height=(\d )\](.*?)\[/img\]%s',
'%\[img=([^\s] )\/\]%s',
'%\[flash width=(\d ) height=(\d )\](.*?)\[/flash\]%se',
'%\[(b|i|u|strike|sup|sub)\](.*?)\[/\https://www.gxlcms.com/1\]%s',
'%\[h([https://www.gxlcms.com/1-6])\](.*?)\[/h\https://www.gxlcms.com/1\]%s',
'%\[hr\/\]%s',
'%\[color=([^\s] )\](.*?)\[/color\]%s',
'%\[font=([^\"] )\](.*?)\[/font\]%s',
'%\[size=([^\s] )\](.*?)\[/size\]%s',
'%\[align=(center|right|left)\](.*?)\[/align\]%s',
'%\[valign=(middle|top|bottom)\](.*?)\[/valign\]%s',
/*
'%\[ul\](.*?)\[/ul\]%s',
'%\[ul=(circle|disc|square)\](.*?)\[/ul\]%s',
'%\[ol\](.*?)\[/ol\]%s',
'%\[ol type=([aAiIhttps://www.gxlcms.com/1]) start=([a-zA-Zhttps://www.gxlcms.com/1-9])\](.*?)\[/ol\]%s',
'%\[li\](.*?)\[/li\]%s',
*/
'%\[table=([^\s] ?)\](.*?)\[/table\]%s',
'%\[caption\](.*?)\[/caption\]%s',
'%\[tr=([^\s] ?)\](.*?)\[/tr\]%s',
'%\[th\](.*?)\[/th\]%s',
'%\[td\](.*?)\[/td\]%s',
'%\[note\](.*?)\[/note\]%s',
'%\[quote=(.*?)\](.*?)\[/quote\]%s',
'%\[code\](.*?)\[/code\]%s',
'%[ ]{2}%s', // make double-spaces truly double-spaces!
);
$replace = array
(
'\2',
'\2',
'',
'',
'sprintf("$flash", "\https://www.gxlcms.com/1", "\2", "\https://www.gxlcms.com/3")',
'<\https://www.gxlcms.com/1>\2\https://www.gxlcms.com/1>',
'\2',
'
\n',
'\2',
'\2',
'\2',
'\2',
'\2',
/*
'\https://www.gxlcms.com/1
',
'',
'\https://www.gxlcms.com/1
',
'\https://www.gxlcms.com/3
',
'\https://www.gxlcms.com/1',
*/
"",
"\t$https://www.gxlcms.com/1\n",
"\t\n$2\t
\n",
"\t\t$https://www.gxlcms.com/1 | \n",
"\t\t$https://www.gxlcms.com/1 | \n",
'发布者备注
\https://www.gxlcms.com/1',
'引用(来源: \https://www.gxlcms.com/1)
\2',
'代码\https://www.gxlcms.com/1',
' ',
);
if( preg_match('%\[table=(.*?)\/table\]%s', $ubb, $tablecells) ) //如果有表格, 先去除单元格之间的多余空白
{
$bb=preg_replace('%\]([\r\n\s]*)\[%si', '][', $tablecells[https://www.gxlcms.com/1]);
$ubb=str_replace($tablecells[https://www.gxlcms.com/1], $bb, $ubb);
}
$html = preg_replace($match, $replace, nl2br(htmlspecialchars($ubb)));
$html = preg_replace('/
\s*<(td|th|tr|table|ul|ol|li)/m', "\n".'<\https://www.gxlcms.com/1', $html);
return $html;
}