標籤:style blog http color 使用 strong 資料 io
1.UBB 轉為 HTML
TP的擴充裡面內建一個ubb方法,用這個方法就能把使用者輸入的ubb格式代碼轉換為HTML標籤的代碼。這裡用到的基本知識就是Regex啦,今天先不講Regex。
來看一下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裡面需要用“/ /”把正則和模式修飾符分開,模式修飾符在後一個“/”右面。兩個"/"中間的才是真正的Regex。
2.HTML 轉為 UBB
由於網站使用UBB代碼是為了提高安全性,同時UBB代碼寫完以後只供瀏覽,很少會寫到把HTML重新轉換為UBB,即使有,見到的更多的也是js代碼,下面我分享一個我在網上找到的方法,基本可行。
當然如果用PHP寫在後台也完全是可以的,鑒於我的Regex還沒有熟練,這裡就先給出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(/&/ig, ‘&‘);18 str = str.replace(/</ig, ‘<‘);19 str = str.replace(/>/ig, ‘>‘);20 }
這個的用法就是傳入含有HTML標籤代碼的字串,方法處理HTML標籤轉為UBB代碼,返迴轉換後的字串。
由於UBB代碼是有限的,在各大網站使用的都不相同,所以對應的轉換方法裡麵包含的也不相同,如果有沒寫到的,自己琢磨一下吧。
其實將HTML代碼轉換為UBB我個人覺得是沒有太大必要的,資料完全可以從使用者輸入的UBB代碼直接儲存到資料庫,需要顯示出來的時候拿出來轉換成HTML就可以了,這樣即使涉及到編輯功能也完全可以HOLD住。不用先把UBB轉成HTML存入資料庫供前台查閱,然後編輯的時候再從HTML轉成UBB讓使用者繼續編輯。
我之所以要寫HTML轉成UBB的類是想看看能不能行的通,為此還專門學了一遍Regex,關於Regex下次再說吧。