โค้ด การใช้ BBCode

ฟังก์ชั่น BBCode รูปแบบหนึ่ง ที่สามารถทำ BBCode ได้หลายแบบ เช่น ตัวหนา ตัวเอียง สี แบบอักษร ลิงค์ต่างๆ รูปภาพ

<?php
function BBCodeFormat($str, $removeTags = FALSE){
    $str = $removeTags ? strip_tags($str) : htmlentities($str);
    $str = nl2br($str);
    $pattern = array(
     '/\[b\](.*?)\[\/b\]/is',
     '/\[i\](.*?)\[\/i\]/is',
     '/\[u\](.*?)\[\/u\]/is',
     '/\[url\=(.*?)\](.*?)\[\/url\]/is',
     '/\[url\](.*?)\[\/url\]/is',
     '/\[align\=(left|center|right)\](.*?)\[\/align\]/is',
     '/\[img\](.*?)\[\/img\]/is',
     '/\[mail\=(.*?)\](.*?)\[\/mail\]/is',
     '/\[mail\](.*?)\[\/mail\]/is',
     '/\[font\=(.*?)\](.*?)\[\/font\]/is',
     '/\[size\=(.*?)\](.*?)\[\/size\]/is',
     '/\[color\=(.*?)\](.*?)\[\/color\]/is',
    );

    $replace = array(
      '<strong>\\1</strong>',
     '<em>\\1</em>',
     '<u>\\1</u>',
     '<a href="\\1">\\2</a>',
     '<a href="\\1">\\1</a>',
     '<div style="text-align: \\1;">\\2</div>',
     '<img src="\\1" />',
     '<a href="mailto:\\1">\\2</a>',
     '<a href="mailto:\\1">\\1</a>',
     '<span style="font-family:\\1;">\\2</span>',
     '<span style="font-size:\\1;">\\2</span>',
     '<span style="color:\\1;">\\2</span>',
    );
    // Replace Tags
    $str = preg_replace ($pattern, $replace, $str);
    // Replace Quotes
    $open = '<blockquote>';
    $close = '</blockquote>';
    preg_match_all ('/\[quote\]/i', $str, $matches);
    $opentags = count($matches['0']);
    preg_match_all ('/\[\/quote\]/i', $str, $matches);
    $closetags = count($matches['0']);
    $unclosed = $opentags - $closetags;
    for ($i = 0; $i < $unclosed; $i++)$str .= '</blockquote>';
    $str = str_replace ('
อ้างอิงจาก หัวข้อ', $open, $str);
    $str = str_replace ('[/' . 'quote]', $close, $str);
    return $str;
}
?>


ตัวอย่าง :
<?php
     $str = 'ตัวหนาตัวเอียงwww.goragod.com';
     echo BBCodeFormat($str);
?>
ผู้เขียน goragod โพสต์เมื่อ 05 มิ.ย. 2552 เปิดดู 9,427 ป้ายกำกับ PHP
^