ubb2html.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /*!
  3. * ubb2html support for php
  4. * @requires xhEditor
  5. *
  6. * @author Yanis.Wang<yanis.wang@gmail.com>
  7. * @site http://xheditor.com/
  8. * @licence LGPL(http://www.opensource.org/licenses/lgpl-license.php)
  9. *
  10. * @Version: 0.9.10 (build 110801)
  11. */
  12. function ubb2html($sUBB)
  13. {
  14. $sHtml=$sUBB;
  15. global $emotPath,$cnum,$arrcode,$bUbb2htmlFunctionInit;$cnum=0;$arrcode=array();
  16. $emotPath='../xheditor_emot/';//������·��
  17. if(!$bUbb2htmlFunctionInit){
  18. function saveCodeArea($match)
  19. {
  20. global $cnum,$arrcode;
  21. $cnum++;$arrcode[$cnum]=$match[0];
  22. return "[\tubbcodeplace_".$cnum."\t]";
  23. }}
  24. $sHtml=preg_replace_callback('/\[code\s*(?:=\s*((?:(?!")[\s\S])+?)(?:"[\s\S]*?)?)?\]([\s\S]*?)\[\/code\]/i','saveCodeArea',$sHtml);
  25. $sHtml=preg_replace("/&/",'&amp;',$sHtml);
  26. $sHtml=preg_replace("/</",'&lt;',$sHtml);
  27. $sHtml=preg_replace("/>/",'&gt;',$sHtml);
  28. $sHtml=preg_replace("/\r?\n/",'<br />',$sHtml);
  29. $sHtml=preg_replace("/\[(\/?)(b|u|i|s|sup|sub)\]/i",'<$1$2>',$sHtml);
  30. $sHtml=preg_replace('/\[color\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\]/i','<span style="color:$1;">',$sHtml);
  31. if(!$bUbb2htmlFunctionInit){
  32. function getSizeName($match)
  33. {
  34. $arrSize=array('10px','13px','16px','18px','24px','32px','48px');
  35. if(preg_match("/^\d+$/",$match[1]))$match[1]=$arrSize[$match[1]-1];
  36. return '<span style="font-size:'.$match[1].';">';
  37. }}
  38. $sHtml=preg_replace_callback('/\[size\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\]/i','getSizeName',$sHtml);
  39. $sHtml=preg_replace('/\[font\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\]/i','<span style="font-family:$1;">',$sHtml);
  40. $sHtml=preg_replace('/\[back\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\]/i','<span style="background-color:$1;">',$sHtml);
  41. $sHtml=preg_replace("/\[\/(color|size|font|back)\]/i",'</span>',$sHtml);
  42. for($i=0;$i<3;$i++)$sHtml=preg_replace('/\[align\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\](((?!\[align(?:\s+[^\]]+)?\])[\s\S])*?)\[\/align\]/','<p align="$1">$2</p>',$sHtml);
  43. $sHtml=preg_replace('/\[img\]\s*(((?!")[\s\S])+?)(?:"[\s\S]*?)?\s*\[\/img\]/i','<img src="$1" alt="" />',$sHtml);
  44. if(!$bUbb2htmlFunctionInit){
  45. function getImg($match)
  46. {
  47. $alt=$match[1];$p1=$match[2];$p2=$match[3];$p3=$match[4];$src=$match[5];
  48. $a=$p3?$p3:(!is_numeric($p1)?$p1:'');
  49. return '<img src="'.$src.'" alt="'.$alt.'"'.(is_numeric($p1)?' width="'.$p1.'"':'').(is_numeric($p2)?' height="'.$p2.'"':'').($a?' align="'.$a.'"':'').' />';
  50. }}
  51. $sHtml=preg_replace_callback('/\[img\s*=([^,\]]*)(?:\s*,\s*(\d*%?)\s*,\s*(\d*%?)\s*)?(?:,?\s*(\w+))?\s*\]\s*(((?!")[\s\S])+?)(?:"[\s\S]*)?\s*\[\/img\]/i','getImg',$sHtml);
  52. if(!$bUbb2htmlFunctionInit){
  53. function getEmot($match)
  54. {
  55. global $emotPath;
  56. $arr=split(',',$match[1]);
  57. if(!isset($arr[1])){$arr[1]=$arr[0];$arr[0]='default';}
  58. $path=$emotPath.$arr[0].'/'.$arr[1].'.gif';
  59. return '<img src="'.$path.'" alt="'.$arr[1].'" />';
  60. }}
  61. $sHtml=preg_replace_callback('/\[emot\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\/\]/i','getEmot',$sHtml);
  62. $sHtml=preg_replace('/\[url\]\s*(((?!")[\s\S])*?)(?:"[\s\S]*?)?\s*\[\/url\]/i','<a href="$1">$1</a>',$sHtml);
  63. $sHtml=preg_replace('/\[url\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\]\s*([\s\S]*?)\s*\[\/url\]/i','<a href="$1">$2</a>',$sHtml);
  64. $sHtml=preg_replace('/\[email\]\s*(((?!")[\s\S])+?)(?:"[\s\S]*?)?\s*\[\/email\]/i','<a href="mailto:$1">$1</a>',$sHtml);
  65. $sHtml=preg_replace('/\[email\s*=\s*([^\]"]+?)(?:"[^\]]*?)?\s*\]\s*([\s\S]+?)\s*\[\/email\]/i','<a href="mailto:$1">$2</a>',$sHtml);
  66. $sHtml=preg_replace("/\[quote\]/i",'<blockquote>',$sHtml);
  67. $sHtml=preg_replace("/\[\/quote\]/i",'</blockquote>',$sHtml);
  68. if(!$bUbb2htmlFunctionInit){
  69. function getFlash($match)
  70. {
  71. $w=$match[1];$h=$match[2];$url=$match[3];
  72. if(!$w)$w=480;if(!$h)$h=400;
  73. return '<embed type="application/x-shockwave-flash" src="'.$url.'" wmode="opaque" quality="high" bgcolor="#ffffff" menu="false" play="true" loop="true" width="'.$w.'" height="'.$h.'" />';
  74. }}
  75. $sHtml=preg_replace_callback('/\[flash\s*(?:=\s*(\d+)\s*,\s*(\d+)\s*)?\]\s*(((?!")[\s\S])+?)(?:"[\s\S]*?)?\s*\[\/flash\]/i','getFlash',$sHtml);
  76. if(!$bUbb2htmlFunctionInit){
  77. function getMedia($match)
  78. {
  79. $w=$match[1];$h=$match[2];$play=$match[3];$url=$match[4];
  80. if(!$w)$w=480;if(!$h)$h=400;
  81. return '<embed type="application/x-mplayer2" src="'.$url.'" enablecontextmenu="false" autostart="'.($play=='1'?'true':'false').'" width="'.$w.'" height="'.$h.'" />';
  82. }}
  83. $sHtml=preg_replace_callback('/\[media\s*(?:=\s*(\d+)\s*,\s*(\d+)\s*(?:,\s*(\d+)\s*)?)?\]\s*(((?!")[\s\S])+?)(?:"[\s\S]*?)?\s*\[\/media\]/i','getMedia',$sHtml);
  84. if(!$bUbb2htmlFunctionInit){
  85. function getTable($match)
  86. {
  87. return '<table'.(isset($match[1])?' width="'.$match[1].'"':'').(isset($match[2])?' bgcolor="'.$match[2].'"':'').'>';
  88. }}
  89. $sHtml=preg_replace_callback('/\[table\s*(?:=(\d{1,4}%?)\s*(?:,\s*([^\]"]+)(?:"[^\]]*?)?)?)?\s*\]/i','getTable',$sHtml);
  90. if(!$bUbb2htmlFunctionInit){
  91. function getTR($match){return '<tr'.(isset($match[1])?' bgcolor="'.$match[1].'"':'').'>';}}
  92. $sHtml=preg_replace_callback('/\[tr\s*(?:=(\s*[^\]"]+))?(?:"[^\]]*?)?\s*\]/i','getTR',$sHtml);
  93. if(!$bUbb2htmlFunctionInit){
  94. function getTD($match){
  95. $col=isset($match[1])?$match[1]:0;$row=isset($match[2])?$match[2]:0;$w=isset($match[3])?$match[3]:null;
  96. return '<td'.($col>1?' colspan="'.$col.'"':'').($row>1?' rowspan="'.$row.'"':'').($w?' width="'.$w.'"':'').'>';
  97. }}
  98. $sHtml=preg_replace_callback("/\[td\s*(?:=\s*(\d{1,2})\s*,\s*(\d{1,2})\s*(?:,\s*(\d{1,4}%?))?)?\s*\]/i",'getTD',$sHtml);
  99. $sHtml=preg_replace("/\[\/(table|tr|td)\]/i",'</$1>',$sHtml);
  100. $sHtml=preg_replace("/\[\*\]((?:(?!\[\*\]|\[\/list\]|\[list\s*(?:=[^\]]+)?\])[\s\S])+)/i",'<li>$1</li>',$sHtml);
  101. if(!$bUbb2htmlFunctionInit){
  102. function getUL($match)
  103. {
  104. $str='<ul';
  105. if(isset($match[1]))$str.=' type="'.$match[1].'"';
  106. return $str.'>';
  107. }}
  108. $sHtml=preg_replace_callback('/\[list\s*(?:=\s*([^\]"]+))?(?:"[^\]]*?)?\s*\]/i','getUL',$sHtml);
  109. $sHtml=preg_replace("/\[\/list\]/i",'</ul>',$sHtml);
  110. $sHtml=preg_replace("/\[hr\/\]/i",'<hr />',$sHtml);
  111. for($i=1;$i<=$cnum;$i++)$sHtml=str_replace("[\tubbcodeplace_".$i."\t]", $arrcode[$i],$sHtml);
  112. if(!$bUbb2htmlFunctionInit){
  113. function fixText($match)
  114. {
  115. $text=$match[2];
  116. $text=preg_replace("/\t/",'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;',$text);
  117. $text=preg_replace("/ /",'&nbsp;',$text);
  118. return $match[1].$text;
  119. }}
  120. $sHtml=preg_replace_callback('/(^|<\/?\w+(?:\s+[^>]*?)?>)([^<$]+)/i','fixText',$sHtml);
  121. $bUbb2htmlFunctionInit=true;
  122. return $sHtml;
  123. }
  124. ?>