ThinkPHP HTML标签代码和UBB互相转换

1.UBB 转为 HTML

TP的扩展里面自带一个ubb方法,用这个方法就能把用户输入的ubb格式代码转换为HTML标签的代码。这里用到的基本知识就是正则表达式啦,今天先不讲正则表达式。

来看一下TP自带的方法,这个类的位置在:\ThinkPHP\Extend\Function\extend.php  ,ubb方法全部代码如下:

 1 function ubb($Text) {
 2   $Text=trim($Text);
 3   //$Text=htmlspecialchars($Text);
 4   $Text=preg_replace("/\\t/is","  ",$Text);
 5   $Text=preg_replace("/\[h1\](.+?)\[\/h1\]/is","<h1>\\1</h1>",$Text);
 6   $Text=preg_replace("/\[h2\](.+?)\[\/h2\]/is","<h2>\\1</h2>",$Text);
 7   $Text=preg_replace("/\[h3\](.+?)\[\/h3\]/is","<h3>\\1</h3>",$Text);
 8   $Text=preg_replace("/\[h4\](.+?)\[\/h4\]/is","<h4>\\1</h4>",$Text);
 9   $Text=preg_replace("/\[h5\](.+?)\[\/h5\]/is","<h5>\\1</h5>",$Text);
10   $Text=preg_replace("/\[h6\](.+?)\[\/h6\]/is","<h6>\\1</h6>",$Text);
11   $Text=preg_replace("/\[separator\]/is","",$Text);
12   $Text=preg_replace("/\[center\](.+?)\[\/center\]/is","<center>\\1</center>",$Text);
13   $Text=preg_replace("/\[url=http:\/\/([^\[]*)\](.+?)\[\/url\]/is","<a href=\"http://\\1\" target=_blank>\\2</a>",$Text);
14   $Text=preg_replace("/\[url=([^\[]*)\](.+?)\[\/url\]/is","<a href=\"http://\\1\" target=_blank>\\2</a>",$Text);
15   $Text=preg_replace("/\[url\]http:\/\/([^\[]*)\[\/url\]/is","<a href=\"http://\\1\" target=_blank>\\1</a>",$Text);
16   $Text=preg_replace("/\[url\]([^\[]*)\[\/url\]/is","<a href=\"\\1\" target=_blank>\\1</a>",$Text);
17   $Text=preg_replace("/\[img\](.+?)\[\/img\]/is","<img src=\\1 />",$Text);
18   $Text=preg_replace("/\[color=(.+?)\](.+?)\[\/color\]/is","<font color=\\1>\\2</font>",$Text);
19   $Text=preg_replace("/\[size=(.+?)\](.+?)\[\/size\]/is","<font size=\\1>\\2</font>",$Text);
20   $Text=preg_replace("/\[sup\](.+?)\[\/sup\]/is","<sup>\\1</sup>",$Text);
21   $Text=preg_replace("/\[sub\](.+?)\[\/sub\]/is","<sub>\\1</sub>",$Text);
22   $Text=preg_replace("/\[pre\](.+?)\[\/pre\]/is","<pre>\\1</pre>",$Text);
23   $Text=preg_replace("/\[email\](.+?)\[\/email\]/is","<a href='mailto:\\1'>\\1</a>",$Text);
24   $Text=preg_replace("/\[colorTxt\](.+?)\[\/colorTxt\]/eis","color_txt('\\1')",$Text);
25   $Text=preg_replace("/\[emot\](.+?)\[\/emot\]/eis","emot('\\1')",$Text);
26   $Text=preg_replace("/\[i\](.+?)\[\/i\]/is","<i>\\1</i>",$Text);
27   $Text=preg_replace("/\[u\](.+?)\[\/u\]/is","<u>\\1</u>",$Text);
28   $Text=preg_replace("/\[b\](.+?)\[\/b\]/is","<b>\\1</b>",$Text);
29   $Text=preg_replace("/\[quote\](.+?)\[\/quote\]/is"," <div class='quote'><h5>引用:</h5><blockquote>\\1</blockquote></div>", $Text);
30   $Text=preg_replace("/\[code\](.+?)\[\/code\]/eis","highlight_code('\\1')", $Text);
31   $Text=preg_replace("/\[php\](.+?)\[\/php\]/eis","highlight_code('\\1')", $Text);
32   $Text=preg_replace("/\[sig\](.+?)\[\/sig\]/is","<div class='sign'>\\1</div>", $Text);
33   $Text=preg_replace("/\\n/is","<br/>",$Text);
34   return $Text;
35 }

功能就是将含有ubb代码的字符串里面的ubb代码转换成HTML标签代码,返回转换完的字符串。

可能因为我比较菜鸟,所以对有一个小的细节有点在意,在这里提醒所有和我一样的菜鸟们一下,就是在ThinkPHP里,有一个叫做“模式修饰符”的概念,就是

$Text=preg_replace("/\[php\](.+?)\[\/php\]/eis","highlight_code('\\1')", $Text);

红色标注的部分,详细内容见:模式修饰符

所以在Thinkphp后台用preg_replace(pattern, replacement, subject)方法进行正则替换的时候需要注意了,参数pattern里面需要用“/ /”把正则和模式修饰符分开,模式修饰符在后一个“/”右面。两个"/"中间的才是真正的正则表达式。

 

2.HTML 转为 UBB

由于网站使用UBB代码是为了提高安全性,同时UBB代码写完以后只供浏览,很少会写到把HTML重新转换为UBB,即使有,见到的更多的也是js代码,下面我分享一个我在网上找到的方法,基本可行。

当然如果用PHP写在后台也完全是可以的,鉴于我的正则表达式还没有熟练,这里就先给出js版本的。

 1     function htmltoubb(str){
 2         str = str.replace(/<br[^>]*>/ig,'\n');
 3         str = str.replace(/<p[^>\/]*\/>/ig,'\n'); 
 4         str = str.replace(/\son[\w]{3,16}\s?=\s*([\'\"]).+?\1/ig,'');
 5         str = str.replace(/<hr[^>]*>/ig,'[hr]');
 6         str = str.replace(/<(sub|sup|u|strike|b|i|pre)>/ig,'[$1]');
 7         str = str.replace(/<\/(sub|sup|u|strike|b|i|pre)>/ig,'[/$1]');
 8         str = str.replace(/<(\/)?b>/ig,'[$1b]');
 9         str = str.replace(/<(\/)?em>/ig,'[$1i]');
10         str = str.replace(/<(\/)?blockquote([^>]*)>/ig,'[$1blockquote]');
11         str = str.replace(/<img[^>]*smile=\"(\d+)\"[^>]*>/ig,'[s:$1]');
12         str = str.replace(/<img[^>]*src=[\'\"\s]*([^\s\'\"]+)[^>]*>/ig,'[img]'+'$1'+'[/img]');
13 
14         str = str.replace(/<a[^>]*href=[\'\"\s]*([^\s\'\"]*)[^>]*>(.+?)<\/a>/ig,'[url]'+'$1'+'[/url]');
15         str = str.replace(/<h([1-6])+>(.+?)<\/h\1>/ig,'[h$1]'+'$2'+'[/h$1]');
16         str = str.replace(/<[^>]*?>/ig, '');
17         str = str.replace(/&amp;/ig, '&');
18         str = str.replace(/&lt;/ig, '<');
19         str = str.replace(/&gt;/ig, '>');
20     }

这个的用法就是传入含有HTML标签代码的字符串,方法处理HTML标签转为UBB代码,返回转换后的字符串。

 

由于UBB代码是有限的,在各大网站使用的都不相同,所以对应的转换方法里面包含的也不相同,如果有没写到的,自己琢磨一下吧。

 

其实将HTML代码转换为UBB我个人觉得是没有太大必要的,数据完全可以从用户输入的UBB代码直接保存到数据库,需要显示出来的时候拿出来转换成HTML就可以了,这样即使涉及到编辑功能也完全可以HOLD住。不用先把UBB转成HTML存入数据库供前台查阅,然后编辑的时候再从HTML转成UBB让用户继续编辑。

我之所以要写HTML转成UBB的类是想看看能不能行的通,为此还专门学了一遍正则表达式,关于正则表达式下次再说吧。

posted @ 2014-07-24 18:02  雏菊之秋  阅读(2187)  评论(0编辑  收藏  举报