35

bbcode

bbcode
SYNOPSIS

bbcode($s)

DESCRIPTION
CODE
  1. require_once 'geshi.php';
  2.  
  3. function bbcode($s) {
  4.     static $bbcode = array(
  5.         '#\[br\]#is'                    => '<br />',
  6. //      '#\[(h[4-6])\](.+?)\[/\1\]#is'  => '<\1>\2</\1>',
  7.         '#\[b\](.+?)\[/b\]#is'          => '<b>\1</b>',
  8.         '#\[i\](.+?)\[/i\]#is'          => '<i>\1</i>',
  9.         '#\[u\](.+?)\[/u\]#is'          => '<u>\1</u>',
  10.         '#\[s\](.+?)\[/s\]#is'          => '<s>\1</s>',
  11.         '#\[p\](.+?)\[/p\]#is'          => '<p>\1</p>',
  12.         '#\[pre\](.+?)\[/pre\]#is'      => '<pre>\1</pre>',
  13.         '#\[quote\](.+?)\[/quote\]#is'  => '<blockquote>\1</blockquote>',
  14. //      '#\[small\](.+?)\[/small\]#is'  => '<span class="smaller">\1</span>',
  15. //      '#\[big\](.+?)\[/big\]#is'      => '<span class="larger">\1</span>',
  16.     );
  17.  
  18.     $s = preg_replace_callback('#\[code([^\]]*?)\](.*?)\[/code\]#is', function ($m) { return '[code' . $m[1] . ']' . bbcode_protect($m[2]) . '[/code]'; }, $s);
  19.  
  20.     $s = htmlspecialchars($s, ENT_COMPAT, 'UTF-8');
  21.  
  22.     $s = preg_replace(array_keys($bbcode), array_values($bbcode), $s);
  23.  
  24.     $bbcode_cb = array(
  25.         '#\[url\=(.+?)\](.*?)\[/url\]#is'       => function($m) { return filter_var($m[1], FILTER_VALIDATE_URL) ? '<a href="' . $m[1] . '">' . $m[2] . '</a>' : $m[0]; },
  26.         '#\[url](.*?)\[/url\]#is'               => function($m) { return filter_var($m[1], FILTER_VALIDATE_URL) ? '<a href="' . $m[1] . '">' . $m[1] . '</a>' : $m[0]; },
  27.         '#\[(e?mail)\=(.+?)\](.*?)\[/\1\]#is'   => function($m) { return filter_var($m[2], FILTER_VALIDATE_EMAIL) ? '<a href="mailto:' . $m[2] .'">' . $m[3] . '</a>' : $m[0]; },
  28.         '#\[(e?mail)\](.*?)\[/\1\]#is'          => function($m) { return filter_var($m[2], FILTER_VALIDATE_EMAIL) ? '<a href="mailto:' . $m[2] .'">' . $m[2] . '</a>' : $m[0]; },
  29.         '#\[code\=(.+?)\](.+?)\[/code\]#is'     => function($m) { return bbcode_highlite($m[2], $m[1]); },
  30.         '#\[code\](.+?)\[/code\]#is'            => function($m) { return bbcode_highlite($m[1]); },
  31.     );
  32.  
  33.     foreach ($bbcode_cb as $regexp => $f) {
  34.         $s = preg_replace_callback($regexp, $f, $s);
  35.     }
  36.  
  37.     return $s;
  38. }
  39.  
  40. function bbcode_protect($s) {
  41.     return base64_encode(preg_replace('#\\\"#', '"', $s));
  42. }
  43.  
  44. function bbcode_highlite($s, $language=false) {
  45.     $s = base64_decode($s);
  46.  
  47.     if (!$language) {
  48.         return '<code>' . htmlspecialchars($s, ENT_COMPAT, 'UTF-8') . '</code>';
  49.     }
  50.  
  51.     $geshi = new GeSHi($s, $language);
  52.     $geshi->enable_classes(true);
  53.     $geshi->set_header_type(GESHI_HEADER_DIV);
  54.     $geshi->enable_keyword_links(false);
  55.     $geshi->set_tab_width(4);
  56.  
  57.     $output = $geshi->parse_code();
  58.  
  59.     if ($geshi->error()) {
  60.         return false;
  61.     }
  62.  
  63.     head('stylesheet', 'geshi/' . $language, 'screen');
  64.  
  65.     return '<div class="geshi">' . $output . '</div>';
  66. }
SEE ALSO

pretty_file

Comments

Your comment:
[p] [b] [i] [u] [s] [quote] [pre] [br] [code] [url] [email] strip help 2000

Enter a maximum of 2000 characters.
Improve the presentation of your text with the following formatting tags:
[p]paragraph[/p], [b]bold[/b], [i]italics[/i], [u]underline[/u], [s]strike[/s], [quote]citation[/quote], [pre]as is[/pre], [br]line break,
[url]http://www.izend.org[/url], [url=http://www.izend.org]site[/url], [email]izend@izend.org[/email], [email=izend@izend.org]izend[/email],
[code]command[/code], [code=language]source code in c, java, php, html, javascript, xml, css, sql, bash, dos, make, etc.[/code].