首页 > 开发 > PHP > 正文

自己写的一个UBB转换的函数

2024-05-04 23:00:05
字体:
来源:转载
供稿:网友
function ubb2xhtml($ubb)
{
$flash=<<<end
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
 codebase="" 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=">
</object>
end;
 $match = array
 (
  @#%/[url=([^/s]+)/](.*?)/[/url/]%[email protected]#,
  @#%/[email=([^/s])+/](.*?)/[/email/]%[email protected]#,
  @#%/[img width=(/d+) height=(/d+)/](.*?)/[/img/]%[email protected]#,
  @#%/[img=([^/s]+)///]%[email protected]#,
  @#%/[flash width=(/d+) height=(/d+)/](.*?)/[/flash/]%[email protected]#,
  @#%/[(b|i|u|strike|sup|sub)/](.*?)/[//1/]%[email protected]#,
  @#%/[h([1-6])/](.*?)/[/h/1/]%[email protected]#,
  @#%/[hr///]%[email protected]#,
  @#%/[color=([^/s]+)/](.*?)/[/color/]%[email protected]#,
  @#%/[font=([^/"]+)/](.*?)/[/font/]%[email protected]#,
  @#%/[size=([^/s]+)/](.*?)/[/size/]%[email protected]#,
  @#%/[align=(center|right|left)/](.*?)/[/align/]%[email protected]#,
  @#%/[valign=(middle|top|bottom)/](.*?)/[/valign/]%[email protected]#,
/*
  @#%/[ul/](.*?)/[/ul/]%[email protected]#,
  @#%/[ul=(circle|disc|square)/](.*?)/[/ul/]%[email protected]#,
  @#%/[ol/](.*?)/[/ol/]%[email protected]#,
  @#%/[ol type=([aaii1]) start=([a-za-z1-9])/](.*?)/[/ol/]%[email protected]#,
  @#%/[li/](.*?)/[/li/]%[email protected]#,
*/
  @#%/[table=([^/s]+?)/](.*?)/[/table/]%[email protected]#,
  @#%/[caption/](.*?)/[/caption/]%[email protected]#,
  @#%/[tr=([^/s]+?)/](.*?)/[/tr/]%[email protected]#,
  @#%/[th/](.*?)/[/th/]%[email protected]#,
  @#%/[td/](.*?)/[/td/]%[email protected]#,
  @#%/[note/](.*?)/[/note/]%[email protected]#,
  @#%/[quote=(.*?)/](.*?)/[/quote/]%[email protected]#,
  @#%/[code/](.*?)/[/code/]%[email protected]#,
  @#%[ ]{2}%[email protected]#,  // make double-spaces truly double-spaces!
 );
    $replace = array
 (
  @#<a href="/1" target="_blank">/2</a>@#,
  @#<a href="" 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>@#,
  @#<h/1>/2</h/1>@#,
  @#<hr>/[email protected]#,
  @#<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$2/t</tr>/n",
  "/t/t<th>$1</th>/n",
  "/t/t<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" >代码</span><div class="code">/1</div>@#,
  @#&nbsp; @#,
 );
 if( preg_match(@#%/[table=(.*?)//table/]%[email protected]#, $ubb, $tablecells) ) //如果有表格, 先去除单元格之间的多余空白
 {
  $bb=preg_replace(@#%/]([/r/n/s]*)/[%[email protected]#, @#][@#, $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)/[email protected]#, "/n"[email protected]#</[email protected]#, $html);
 return $html;
}
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表