Advertisement
samclarko

bbcode parser

Dec 25th, 2015
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.55 KB | None | 0 0
  1. <?php
  2.  
  3. class bbcode {
  4.  
  5.     public static function tohtml($text, $advanced = FALSE, $charset = 'UTF-8') {
  6.  
  7.         //special chars
  8.         $text = htmlspecialchars($text, ENT_QUOTES, $charset);
  9.  
  10.         /**
  11.          * This array contains the main static bbcode
  12.          * @var array $basic_bbcode
  13.          */
  14.         $basic_bbcode = array(
  15.             '[b]', '[/b]',
  16.             '[i]', '[/i]',
  17.             '[u]', '[/u]',
  18.             '[s]', '[/s]',
  19.             '[ul]', '[/ul]',
  20.             '[li]', '[/li]',
  21.             '[ol]', '[/ol]',
  22.             '[code]', '[/code]',
  23.             '[center]', '[/center]',
  24.             '[left]', '[/left]',
  25.             '[right]', '[/right]',
  26.            
  27.         );
  28.  
  29.         /**
  30.          * This array contains the main static bbcode's html
  31.          * @var array $basic_html
  32.          */
  33.         $basic_html = array(
  34.             '<b>', '</b>',
  35.             '<i>', '</i>',
  36.             '<u>', '</u>',
  37.             '<s>', '</s>',
  38.             '<ul>', '</ul>',
  39.             '<li>', '</li>',
  40.             '<ol>', '</ol>',
  41.             '<div id="code">', '</div>',
  42.             '<div style="text-align: center;">', '</div>',
  43.             '<div style="text-align: left;">', '</div>',
  44.             '<div style="text-align: right;">', '</div>',
  45.            
  46.         );
  47.  
  48.         /**
  49.          *
  50.          * Parses basic bbcode, used str_replace since seems to be the fastest
  51.          */
  52.         $text = str_replace($basic_bbcode, $basic_html, $text);
  53.  
  54.         //advanced BBCODE
  55.         if ($advanced) {
  56.             /**
  57.              * This array contains the advanced static bbcode
  58.              * @var array $advanced_bbcode
  59.              */
  60.             $advanced_bbcode = array(
  61.                 '#\[color=([a-zA-Z]*|\#?[0-9a-fA-F]{6})](.+)\[/color\]#Usi',
  62.                 '#\[size=([0-9][0-9]?)](.+)\[/size\]#Usi',
  63.                 '#\[quote](\r\n)?(.+?)\[/quote]#si',
  64.                 '#\[quote=(.*?)](\r\n)?(.+?)\[/quote]#si',
  65.                 '#\[url](.+)\[/url]#Usi',
  66.                 '#\[url=(.+)](.+)\[/url\]#Usi',
  67.                 '#\[email]([\w\.\-]+@[a-zA-Z0-9\-]+\.?[a-zA-Z0-9\-]*\.\w{1,4})\[/email]#Usi',
  68.                 '#\[email=([\w\.\-]+@[a-zA-Z0-9\-]+\.?[a-zA-Z0-9\-]*\.\w{1,4})](.+)\[/email]#Usi',
  69.                 '#\[img](.+)\[/img]#Usi',
  70.                 '#\[img=(.+)](.+)\[/img]#Usi',
  71.                 '#\[code](\r\n)?(.+?)(\r\n)?\[/code]#si',
  72.                 '#\[youtube]http://[a-z]{0,3}.youtube.com/watch\?v=([0-9a-zA-Z]{1,11})\[/youtube]#Usi',
  73.                 '#\[youtube]([0-9a-zA-Z]{1,11})\[/youtube]#Usi'
  74.             );
  75.  
  76.             /**
  77.              * This array contains the advanced static bbcode's html
  78.              * @var array $advanced_html
  79.              */
  80.             $advanced_html = array(
  81.                 '<span style="color: $1">$2</span>',
  82.                 '<span style="font-size: $1px">$2</span>',
  83.                 "<div class=\"quote\"><span class=\"quoteby\">Quote:</span>\r\n$2</div>",
  84.                 "<div class=\"quote\"><span class=\"quoteby\"><b>$1</b> Wrote:</span>\r\n$3</div>",
  85.                 '<a rel="nofollow" target="_blank" href="$1">$1</a>',
  86.                 '<a rel="nofollow" target="_blank" href="$1">$2</a>',
  87.                 '<a href="mailto: $1">$1</a>',
  88.                 '<a href="mailto: $1">$2</a>',
  89.                 '<img src="$1" alt="$1" />',
  90.                 '<img src="$1" alt="$2" />',
  91.                 '<div class="code">$2</div>',
  92.                 '<object type="application/x-shockwave-flash" style="width: 450px; height: 366px;" data="http://www.youtube.com/v/$1"><param name="movie" value="http://www.youtube.com/v/$1" /><param name="wmode" value="transparent" /></object>',
  93.                 '<object type="application/x-shockwave-flash" style="width: 450px; height: 366px;" data="http://www.youtube.com/v/$1"><param name="movie" value="http://www.youtube.com/v/$1" /><param name="wmode" value="transparent" /></object>'
  94.             );
  95.  
  96.             $text = preg_replace($advanced_bbcode, $advanced_html, $text);
  97.         }
  98.  
  99.         //before return convert line breaks to HTML
  100.         return bbcode::nl2br($text);
  101.     }
  102.  
  103.    
  104.     //+++++++++++++++++++++++++++++++++++++
  105.    
  106.     public static function remove($text) {
  107.         return strip_tags(str_replace(array('[', ']'), array('<', '>'), $text));
  108.     }
  109.  
  110.     /**
  111.      *
  112.      * Inserts HTML line breaks before all newlines in a string
  113.      * @param string $var
  114.      */
  115.     public static function nl2br($var) {
  116.         return str_replace(array('\\r\\n', '\r\\n', 'r\\n', '\r\n', '\n', '\r'), '<br />', nl2br($var));
  117.     }
  118.  
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement