關於 PHP, Linux, Open Source 及個人生活記載的網誌。
RSS icon
  • PHP 編寫 BBCode 函式

    Posted on May 27th, 2007 Sam Tang 1 comment

    現在一些主流的論壇或 Blog 回應,也是用 BBCode 作為更改文字式樣的語法。因為 BBCode 一般上只可以定義粗體、斜體、加底線、文字顏色及插入超連結等,而不可以插入 html tags 及 javascript,以下是用 PHP 製作 BBCode 函式的方法。

    首先第一步是要將 html tags 轉換,例如 '<' 轉換成 '&lt;',這個可以用 htmlentities() 函式實現:

    PHP:
    1. <?php
    2. function bbcode_convert($string){
    3.     return htmlentities($string, ENT_QUOTES);
    4. }
    5. ?>

    然後便要編寫字體式樣的部份,包括有[b]、[i]、[u]、[ur] 及 [img],這裡開始要用正規表達式來做:

    PHP:
    1. <?php
    2. function bbcode_convert($string){
    3.     // 移除 HTML tags
    4.     $string = htmlentities($string, ENT_QUOTES);
    5.  
    6.     // 處理 [b] 及 [/b] 的粗體字
    7.     $string = preg_replace ('/\[b\](.*?)\[\/b\]/is', '<strong>$1</strong>', $string);
    8.     // 處理 [i] 及 [/i] 的斜體字
    9.     $string = preg_replace ('/\[i\](.*?)\[\/i\]/is', '<em>$1</em>', $string);
    10.     // 處理 [u] 及 [/u] 的底線字
    11.     $string = preg_replace ('/\[u\](.*?)\[\/u\]/is', '<u>$1</u>', $string);
    12.  
    13.     // [url] 超連結
    14.     $string = preg_replace('/\[url\](.*?)\[\/url\]/is', '<a href="$1">$1</a>', $string);
    15.     // [url=url] 超連結
    16.     $string = preg_replace('/\[url\=(.*?)\](.*?)\[\/url\]/is', '<a href="$1">$2</a>', $string);
    17.  
    18.     // [img] 圖片
    19.     $string = preg_replace('/\[img\](.*?)\[\/img\]/is', '<img src="$1" />', $string);
    20.  
    21.     return $string;
    22. }
    23. ?>

    現在已經完成了最基本的 BBCode,但可以將以上程式碼簡化,還可以選進執行效能。以上總共呼叫了 6 次 preg_replace() 函式,而實際上 preg_replace() 是可以用 array 作為搜索字串及替換字串的,所以可以改為只呼叫 preg_replace() 函式兩次,那麼程式碼會這樣:

    PHP:
    1. <?php
    2. function bbcode_convert($string){
    3.     // 移除 HTML tags
    4.     $string = htmlentities($string, ENT_QUOTES);
    5.  
    6.     $bbcode_search = array(
    7.                 '/\[b\](.*?)\[\/b\]/is',
    8.                 '/\[i\](.*?)\[\/i\]/is',
    9.                 '/\[u\](.*?)\[\/u\]/is',
    10.                 '/\[url\=(.*?)\](.*?)\[\/url\]/is',
    11.                 '/\[url\](.*?)\[\/url\]/is',
    12.                 '/\[img\](.*?)\[\/img\]/is'
    13.                 );
    14.  
    15.     $bbcode_replace = array(
    16.                 '<strong>$1</strong>',
    17.                 '<em>$1</em>',
    18.                 '<u>$1</u>',
    19.                 '<a href="$1">$2</a>',
    20.                 '<a href="$1">$1</a>',
    21.                 '<img src="$1" />'
    22.                 );
    23.  
    24.     return preg_replace($bbcode_search, $bbcode_replace, $string);
    25. }
    26. ?>

    這個 BBCode 函式已經大致上完成,你可以按照自己的需要加入各種語法,原理與以上的做法相同。


     

    One response to “PHP 編寫 BBCode 函式”

    1. 我有用上面寫的語法,但是繁體字都變成亂碼了!!!!這是什麼原因呢

    Leave a reply

    *
    To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
    Click to hear an audio file of the anti-spam word