自己写的一个UBB转换的函数
类别: PHP教程
function ubb2xhtml($ubb)
{
$flash=<<<END
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="%1$d" height="%2$d">
<param name="movie" value="%3$s" />
<param name="quality" value="high" />
<embed src="%3$s" width="%1$d" height="%2$d" quality="high" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
</object>
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)](.*?)[/1]%s',
'%[h([1-6])](.*?)[/h1]%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=([aAiI1]) start=([a-zA-Z1-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
(
'<a href="1" target="_blank">2</a>',
'<a href="mailto:1" target="_blank">2</a>',
'<img src="3" width="1" height="2" border="0" />',
'<img src="1" border="0" />',
'sprintf("$flash", "1", "2", "3")',
'<1>2</1>',
'<h1>2</h1>',
'<hr>n',
'<font color="1">2</font>',
'<font face="1">2</font>',
'<font size="1">2</font>',
'<div align="1">2</font>',
'<div valign="1">2</font>',
/*
'<ul>1</ul>',
'<ul type="1">2</ul>',
'<ol>1</ol>',
'<ol type="1" start="2">3</ol>',
'<li>1</li>',
*/
"<table class="$1" cellspacing="1">n$2</table>",
"t<caption>$1</caption>n",
"t<tr class="$1">n$2t</tr>n",
"tt<th>$1</th>n",
"tt<td>$1</td>n",
'<div class="note"><span class="hint">发布者备注</span><hr />1</div>',
'<div class="quote"><span class="hint">引用</span>(来源: <a href="1" target="_blank">1</a>)<br />2</div>',
'<span class="hint" style="margin-left: 10px">代码</span><div class="code">1</div>',
' ',
);
if( preg_match('%[table=(.*?)/table]%s', $ubb, $tablecells) ) //如果有表格, 先去除单元格之间的多余空白
{
$bb=preg_replace('%]([rns]*)[%si', '][', $tablecells[1]);
$ubb=str_replace($tablecells[1], $bb, $ubb);
}
$html = preg_replace($match, $replace, nl2br(htmlspecialchars($ubb)));
$html = preg_replace('/<br />s*<(td|th|tr|table|ul|ol|li)/m', "n".'<1', $html);
return $html;
}
{
$flash=<<<END
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="%1$d" height="%2$d">
<param name="movie" value="%3$s" />
<param name="quality" value="high" />
<embed src="%3$s" width="%1$d" height="%2$d" quality="high" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>
</object>
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)](.*?)[/1]%s',
'%[h([1-6])](.*?)[/h1]%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=([aAiI1]) start=([a-zA-Z1-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
(
'<a href="1" target="_blank">2</a>',
'<a href="mailto:1" target="_blank">2</a>',
'<img src="3" width="1" height="2" border="0" />',
'<img src="1" border="0" />',
'sprintf("$flash", "1", "2", "3")',
'<1>2</1>',
'<h1>2</h1>',
'<hr>n',
'<font color="1">2</font>',
'<font face="1">2</font>',
'<font size="1">2</font>',
'<div align="1">2</font>',
'<div valign="1">2</font>',
/*
'<ul>1</ul>',
'<ul type="1">2</ul>',
'<ol>1</ol>',
'<ol type="1" start="2">3</ol>',
'<li>1</li>',
*/
"<table class="$1" cellspacing="1">n$2</table>",
"t<caption>$1</caption>n",
"t<tr class="$1">n$2t</tr>n",
"tt<th>$1</th>n",
"tt<td>$1</td>n",
'<div class="note"><span class="hint">发布者备注</span><hr />1</div>',
'<div class="quote"><span class="hint">引用</span>(来源: <a href="1" target="_blank">1</a>)<br />2</div>',
'<span class="hint" style="margin-left: 10px">代码</span><div class="code">1</div>',
' ',
);
if( preg_match('%[table=(.*?)/table]%s', $ubb, $tablecells) ) //如果有表格, 先去除单元格之间的多余空白
{
$bb=preg_replace('%]([rns]*)[%si', '][', $tablecells[1]);
$ubb=str_replace($tablecells[1], $bb, $ubb);
}
$html = preg_replace($match, $replace, nl2br(htmlspecialchars($ubb)));
$html = preg_replace('/<br />s*<(td|th|tr|table|ul|ol|li)/m', "n".'<1', $html);
return $html;
}
- 上一篇: PHP + AJAX
- 下一篇: PHP的目录管理函数
-= 资 源 教 程 =-
文 章 搜 索