bbcode.php 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /*
  3. * This file is part of SSB. code was used from phpit.net
  4. * Modified by Chris Dorman
  5. * Removed some stuff to make it simpler.
  6. */
  7. // based on http://www.phpit.net/article/create-bbcode-php/
  8. // modified by www.vision.to
  9. // please keep credits, thank you :-)
  10. // document your changes.
  11. function bbcode_format($str) {
  12. $simple_search = array(
  13. '/\[b\](.*?)\[\/b\]/is',
  14. '/\*\*\*(.*?)\*\*\*/is',
  15. '/\[i\](.*?)\[\/i\]/is',
  16. '/\[u\](.*?)\[\/u\]/is',
  17. '/___(.*?)___/is',
  18. '/\[url\=(.*?)\](.*?)\[\/url\]/is',
  19. '/\[img\](.*?)\[\/img\]/is',
  20. '/\[url\](.*?)\[\/url\]/is',
  21. '/\[font\=(.*?)\](.*?)\[\/font\]/is',
  22. '/\[color\=(.*?)\](.*?)\[\/color\]/is',
  23. );
  24. $simple_replace = array(
  25. "<strong>$1</strong>",
  26. "<strong>$1</strong>",
  27. "<em>$1</em>",
  28. "<u>$1</u>",
  29. "<u>$1</u>",
  30. "<a href='$1' title='$2 - $1'>$2</a>",
  31. "<a href='$1'><img src='$1' class='attachment_chat' /></a>",
  32. "<a href='$1' title='$1'>$1</a>",
  33. "<span style='font-family: $1;'>$2</span>",
  34. "<span style='color: $1;'>$2</span>",
  35. );
  36. // Do simple BBCode's
  37. $str = preg_replace ($simple_search, $simple_replace, $str);
  38. // Do <blockquote> BBCode
  39. $str = bbcode_quote ($str);
  40. return $str;
  41. }
  42. function bbcode_quote ($str) {
  43. //added div and class for quotes
  44. $open = '<blockquote>';
  45. $close = '</blockquote>';
  46. // How often is the open tag?
  47. preg_match_all ('/\[quote\]/i', $str, $matches);
  48. $opentags = count($matches['0']);
  49. // How often is the close tag?
  50. preg_match_all ('/\[\/quote\]/i', $str, $matches);
  51. $closetags = count($matches['0']);
  52. // Check how many tags have been unclosed
  53. // And add the unclosing tag at the end of the message
  54. $unclosed = $opentags - $closetags;
  55. for ($i = 0; $i < $unclosed; $i++) {
  56. $str .= '</div></blockquote>';
  57. }
  58. // Do replacement
  59. $str = str_replace ('[' . 'quote]', $open, $str);
  60. $str = str_replace ('[/' . 'quote]', $close, $str);
  61. return $str;
  62. }
  63. ?>