bbcode.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 convertYoutubeURL($string) {
  12. preg_match(
  13. '/[\\?\\&]v=([^\\?\\&]+)/',
  14. $string,
  15. $matches
  16. );
  17. $id = $matches[0];
  18. return $id;
  19. }
  20. function bbcode_format($str) {
  21. $simple_search = array(
  22. '/\[b\](.*?)\[\/b\]/is',
  23. '/\*\*\*(.*?)\*\*\*/is',
  24. '/\[i\](.*?)\[\/i\]/is',
  25. '/\[u\](.*?)\[\/u\]/is',
  26. '/___(.*?)___/is',
  27. '/\[url\=(.*?)\](.*?)\[\/url\]/is',
  28. '/\[img\](.*?)\[\/img\]/is',
  29. '/\[url\](.*?)\[\/url\]/is',
  30. '/\[font\=(.*?)\](.*?)\[\/font\]/is',
  31. '/\[color\=(.*?)\](.*?)\[\/color\]/is',
  32. '~\[youtube]https?.*?(?:[/?&](?:e|vi?|ci)(?:[/=]|%3D)|youtu\.be/|embed/|/user/[^/]+#p/(?:[^/]+/)+)([\w-]{10,12}).*?\[/youtube]~i'
  33. );
  34. $simple_replace = array(
  35. "<strong>$1</strong>",
  36. "<strong>$1</strong>",
  37. "<em>$1</em>",
  38. "<u>$1</u>",
  39. "<u>$1</u>",
  40. "<a href='$1' title='$2 - $1'>$2</a>",
  41. "<a href='$1'><img src='$1' class='attachment_chat' /></a>",
  42. "<a href='$1' title='$1'>$1</a>",
  43. "<span style='font-family: $1;'>$2</span>",
  44. "<span style='color: $1;'>$2</span>",
  45. "<iframe width='560' height='315' src='https://youtube.com/embed/$1' frameborder='0' allow='accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture' allowfullscreen></iframe>",
  46. );
  47. // Do simple BBCode's
  48. $str = preg_replace ($simple_search, $simple_replace, $str);
  49. // Do <blockquote> BBCode
  50. $str = bbcode_quote ($str);
  51. return $str;
  52. }
  53. function bbcode_quote ($str) {
  54. //added div and class for quotes
  55. $open = '<blockquote>';
  56. $close = '</blockquote>';
  57. // How often is the open tag?
  58. preg_match_all ('/\[quote\]/i', $str, $matches);
  59. $opentags = count($matches['0']);
  60. // How often is the close tag?
  61. preg_match_all ('/\[\/quote\]/i', $str, $matches);
  62. $closetags = count($matches['0']);
  63. // Check how many tags have been unclosed
  64. // And add the unclosing tag at the end of the message
  65. $unclosed = $opentags - $closetags;
  66. for ($i = 0; $i < $unclosed; $i++) {
  67. $str .= '</div></blockquote>';
  68. }
  69. // Do replacement
  70. $str = str_replace ('[' . 'quote]', $open, $str);
  71. $str = str_replace ('[/' . 'quote]', $close, $str);
  72. return $str;
  73. }
  74. ?>