2013-12-13 58 views
-1

我在smartys網站上發現了這個Smarty修改器,該修改器是一個將bbcode轉換爲html的帖子,但是我找不到或者沒有說明使用它的語法是什麼。使用Smarty修改器

{$bbcode_here|bbcode2html} 

有人請告訴或告訴我如何使用此Smarty修改器是在下面。

function smarty_modifier_bbcode2html($message) { 
    $preg = array(
      '/(?<!\\\\)\[color(?::\w+)?=(.*?)\](.*?)\[\/color(?::\w+)?\]/si' => "<span style=\"color:\\1\">\\2</span>", 
      '/(?<!\\\\)\[size(?::\w+)?=(.*?)\](.*?)\[\/size(?::\w+)?\]/si'  => "<span style=\"font-size:\\1\">\\2</span>", 
      '/(?<!\\\\)\[font(?::\w+)?=(.*?)\](.*?)\[\/font(?::\w+)?\]/si'  => "<span style=\"font-family:\\1\">\\2</span>", 
      '/(?<!\\\\)\[align(?::\w+)?=(.*?)\](.*?)\[\/align(?::\w+)?\]/si' => "<div style=\"text-align:\\1\">\\2</div>", 
      '/(?<!\\\\)\[b(?::\w+)?\](.*?)\[\/b(?::\w+)?\]/si'     => "<span style=\"font-weight:bold\">\\1</span>", 
      '/(?<!\\\\)\[i(?::\w+)?\](.*?)\[\/i(?::\w+)?\]/si'     => "<span style=\"font-style:italic\">\\1</span>", 
      '/(?<!\\\\)\[u(?::\w+)?\](.*?)\[\/u(?::\w+)?\]/si'     => "<span style=\"text-decoration:underline\">\\1</span>", 
      '/(?<!\\\\)\[center(?::\w+)?\](.*?)\[\/center(?::\w+)?\]/si'  => "<div style=\"text-align:center\">\\1</div>", 

      // [code] & [php] 
      '/(?<!\\\\)\[code(?::\w+)?\](.*?)\[\/code(?::\w+)?\]/si'   => "<div class=\"bb-code\">\\1</div>", 
      '/(?<!\\\\)\[php(?::\w+)?\](.*?)\[\/php(?::\w+)?\]/si'    => "<div class=\"bb-php\">\\1</div>", 
      // [email] 
      '/(?<!\\\\)\[email(?::\w+)?\](.*?)\[\/email(?::\w+)?\]/si'   => "<a href=\"mailto:\\1\" class=\"bb-email\">\\1</a>", 
      '/(?<!\\\\)\[email(?::\w+)?=(.*?)\](.*?)\[\/email(?::\w+)?\]/si' => "<a href=\"mailto:\\1\" class=\"bb-email\">\\2</a>", 
      // [url] 
      '/(?<!\\\\)\[url(?::\w+)?\]www\.(.*?)\[\/url(?::\w+)?\]/si'  => "<a href=\"http://www.\\1\" target=\"_blank\" class=\"bb-url\">\\1</a>", 
      '/(?<!\\\\)\[url(?::\w+)?\](.*?)\[\/url(?::\w+)?\]/si'    => "<a href=\"\\1\" target=\"_blank\" class=\"bb-url\">\\1</a>", 
      '/(?<!\\\\)\[url(?::\w+)?=(.*?)?\](.*?)\[\/url(?::\w+)?\]/si'  => "<a href=\"\\1\" target=\"_blank\" class=\"bb-url\">\\2</a>", 
      // [img] 
      '/(?<!\\\\)\[img(?::\w+)?\](.*?)\[\/img(?::\w+)?\]/si'    => "<img src=\"\\1\" alt=\"\\1\" class=\"bb-image\" />", 
      '/(?<!\\\\)\[img(?::\w+)?=(.*?)x(.*?)\](.*?)\[\/img(?::\w+)?\]/si' => "<img width=\"\\1\" height=\"\\2\" src=\"\\3\" alt=\"\\3\" class=\"bb-image\" />", 
      // [quote] 
      '/(?<!\\\\)\[quote(?::\w+)?\](.*?)\[\/quote(?::\w+)?\]/si'   => "<div>Quote:<div class=\"bb-quote\">\\1</div></div>", 
      '/(?<!\\\\)\[quote(?::\w+)?=(?:&quot;|"|\')?(.*?)["\']?(?:&quot;|"|\')?\](.*?)\[\/quote\]/si' => "<div>Quote \\1:<div class=\"bb-quote\">\\2</div></div>", 
      // [list] 
      '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\*(?::\w+)?\](.*?)(?=(?:\s*<br\s*\/?>\s*)?\[\*|(?:\s*<br\s*\/?>\s*)?\[\/?list)/si' => "\n<li class=\"bb-listitem\">\\1</li>", 
      '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/list(:(?!u|o)\w+)?\](?:<br\s*\/?>)?/si' => "\n</ul>", 
      '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/list:u(:\w+)?\](?:<br\s*\/?>)?/si'   => "\n</ul>", 
      '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/list:o(:\w+)?\](?:<br\s*\/?>)?/si'   => "\n</ol>", 
      '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(:(?!u|o)\w+)?\]\s*(?:<br\s*\/?>)?/si' => "\n<ul class=\"bb-list-unordered\">", 
      '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list:u(:\w+)?\]\s*(?:<br\s*\/?>)?/si'  => "\n<ul class=\"bb-list-unordered\">", 
      '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list:o(:\w+)?\]\s*(?:<br\s*\/?>)?/si'  => "\n<ol class=\"bb-list-ordered\">", 
      '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(?::o)?(:\w+)?=1\]\s*(?:<br\s*\/?>)?/si' => "\n<ol class=\"bb-list-ordered,bb-list-ordered-d\">", 
      '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(?::o)?(:\w+)?=i\]\s*(?:<br\s*\/?>)?/s' => "\n<ol class=\"bb-list-ordered,bb-list-ordered-lr\">", 
      '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(?::o)?(:\w+)?=I\]\s*(?:<br\s*\/?>)?/s' => "\n<ol class=\"bb-list-ordered,bb-list-ordered-ur\">", 
      '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(?::o)?(:\w+)?=a\]\s*(?:<br\s*\/?>)?/s' => "\n<ol class=\"bb-list-ordered,bb-list-ordered-la\">", 
      '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(?::o)?(:\w+)?=A\]\s*(?:<br\s*\/?>)?/s' => "\n<ol class=\"bb-list-ordered,bb-list-ordered-ua\">", 
      // escaped tags like \[b], \[color], \[url], ... 
      '/\\\\(\[\/?\w+(?::\w+)*\])/'          => "\\1" 

); 
    $message = preg_replace(array_keys($preg), array_values($preg), $message); 
    return $message; 
} 

感謝

回答

1

正確使用這個函數將首先將代碼放置在一個PHP文件中,該文件將被調用smarty模板加載。我假設你有聰明的所有設置和運行。

你將不得不改變函數參數只是一點點,如下所示:

function smarty_modifier_bbcode2html($params, &$smarty) { 
    $message = $params['message']; 
    $preg = array(
     '/(?<!\\\\)\[color(?::\w+)?=(.*?)\](.*?)\[\/color(?::\w+)?\]/si' => "<span style=\"color:\\1\">\\2</span>", 
     '/(?<!\\\\)\[size(?::\w+)?=(.*?)\](.*?)\[\/size(?::\w+)?\]/si'  => "<span style=\"font-size:\\1\">\\2</span>", 
     '/(?<!\\\\)\[font(?::\w+)?=(.*?)\](.*?)\[\/font(?::\w+)?\]/si'  => "<span style=\"font-family:\\1\">\\2</span>", 
     '/(?<!\\\\)\[align(?::\w+)?=(.*?)\](.*?)\[\/align(?::\w+)?\]/si' => "<div style=\"text-align:\\1\">\\2</div>", 
     '/(?<!\\\\)\[b(?::\w+)?\](.*?)\[\/b(?::\w+)?\]/si'     => "<span style=\"font-weight:bold\">\\1</span>", 
     '/(?<!\\\\)\[i(?::\w+)?\](.*?)\[\/i(?::\w+)?\]/si'     => "<span style=\"font-style:italic\">\\1</span>", 
     '/(?<!\\\\)\[u(?::\w+)?\](.*?)\[\/u(?::\w+)?\]/si'     => "<span style=\"text-decoration:underline\">\\1</span>", 
     '/(?<!\\\\)\[center(?::\w+)?\](.*?)\[\/center(?::\w+)?\]/si'  => "<div style=\"text-align:center\">\\1</div>", 

     // [code] & [php] 
     '/(?<!\\\\)\[code(?::\w+)?\](.*?)\[\/code(?::\w+)?\]/si'   => "<div class=\"bb-code\">\\1</div>", 
     '/(?<!\\\\)\[php(?::\w+)?\](.*?)\[\/php(?::\w+)?\]/si'    => "<div class=\"bb-php\">\\1</div>", 
     // [email] 
     '/(?<!\\\\)\[email(?::\w+)?\](.*?)\[\/email(?::\w+)?\]/si'   => "<a href=\"mailto:\\1\" class=\"bb-email\">\\1</a>", 
     '/(?<!\\\\)\[email(?::\w+)?=(.*?)\](.*?)\[\/email(?::\w+)?\]/si' => "<a href=\"mailto:\\1\" class=\"bb-email\">\\2</a>", 
     // [url] 
     '/(?<!\\\\)\[url(?::\w+)?\]www\.(.*?)\[\/url(?::\w+)?\]/si'  => "<a href=\"http://www.\\1\" target=\"_blank\" class=\"bb-url\">\\1</a>", 
     '/(?<!\\\\)\[url(?::\w+)?\](.*?)\[\/url(?::\w+)?\]/si'    => "<a href=\"\\1\" target=\"_blank\" class=\"bb-url\">\\1</a>", 
     '/(?<!\\\\)\[url(?::\w+)?=(.*?)?\](.*?)\[\/url(?::\w+)?\]/si'  => "<a href=\"\\1\" target=\"_blank\" class=\"bb-url\">\\2</a>", 
     // [img] 
     '/(?<!\\\\)\[img(?::\w+)?\](.*?)\[\/img(?::\w+)?\]/si'    => "<img src=\"\\1\" alt=\"\\1\" class=\"bb-image\" />", 
     '/(?<!\\\\)\[img(?::\w+)?=(.*?)x(.*?)\](.*?)\[\/img(?::\w+)?\]/si' => "<img width=\"\\1\" height=\"\\2\" src=\"\\3\" alt=\"\\3\" class=\"bb-image\" />", 
     // [quote] 
     '/(?<!\\\\)\[quote(?::\w+)?\](.*?)\[\/quote(?::\w+)?\]/si'   => "<div>Quote:<div class=\"bb-quote\">\\1</div></div>", 
     '/(?<!\\\\)\[quote(?::\w+)?=(?:&quot;|"|\')?(.*?)["\']?(?:&quot;|"|\')?\](.*?)\[\/quote\]/si' => "<div>Quote \\1:<div class=\"bb-quote\">\\2</div></div>", 
     // [list] 
     '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\*(?::\w+)?\](.*?)(?=(?:\s*<br\s*\/?>\s*)?\[\*|(?:\s*<br\s*\/?>\s*)?\[\/?list)/si' => "\n<li class=\"bb-listitem\">\\1</li>", 
     '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/list(:(?!u|o)\w+)?\](?:<br\s*\/?>)?/si' => "\n</ul>", 
     '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/list:u(:\w+)?\](?:<br\s*\/?>)?/si'   => "\n</ul>", 
     '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/list:o(:\w+)?\](?:<br\s*\/?>)?/si'   => "\n</ol>", 
     '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(:(?!u|o)\w+)?\]\s*(?:<br\s*\/?>)?/si' => "\n<ul class=\"bb-list-unordered\">", 
     '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list:u(:\w+)?\]\s*(?:<br\s*\/?>)?/si'  => "\n<ul class=\"bb-list-unordered\">", 
     '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list:o(:\w+)?\]\s*(?:<br\s*\/?>)?/si'  => "\n<ol class=\"bb-list-ordered\">", 
     '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(?::o)?(:\w+)?=1\]\s*(?:<br\s*\/?>)?/si' => "\n<ol class=\"bb-list-ordered,bb-list-ordered-d\">", 
     '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(?::o)?(:\w+)?=i\]\s*(?:<br\s*\/?>)?/s' => "\n<ol class=\"bb-list-ordered,bb-list-ordered-lr\">", 
     '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(?::o)?(:\w+)?=I\]\s*(?:<br\s*\/?>)?/s' => "\n<ol class=\"bb-list-ordered,bb-list-ordered-ur\">", 
     '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(?::o)?(:\w+)?=a\]\s*(?:<br\s*\/?>)?/s' => "\n<ol class=\"bb-list-ordered,bb-list-ordered-la\">", 
     '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[list(?::o)?(:\w+)?=A\]\s*(?:<br\s*\/?>)?/s' => "\n<ol class=\"bb-list-ordered,bb-list-ordered-ua\">", 
     // escaped tags like \[b], \[color], \[url], ... 
     '/\\\\(\[\/?\w+(?::\w+)*\])/'          => "\\1" 

    ); 
    $message = preg_replace(array_keys($preg), array_values($preg), $message); 
    return $message; 
} 

,你將需要註冊的功能,你的PHP文件中,通常在php文件中的函數之後註冊功能因爲這樣:

$smarty->register_block('bbcode2html', 'smarty_modifier_bbcode2html'); 

現在,您可以使用此您的Smarty的模板中,如下圖所示:

<p>{bbcode2html message=$messageToConvert}</p>