Posted By

cjmling on 10/22/10


Tagged

php code bb


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

cjmling


BB Code


 / Published in: PHP
 

  1. function bbcode_format ($str) {
  2. // $str = htmlentities($str); ภาษาไทยจะเพี้ยน
  3. $str = htmlspecialchars($str);
  4.  
  5. $simple_search = array(
  6. //added line break
  7. '/\[br\]/is',
  8. '/\[b\](.*?)\[\/b\]/is',
  9. '/\[i\](.*?)\[\/i\]/is',
  10. '/\[u\](.*?)\[\/u\]/is',
  11. '/\[url\=(.*?)\](.*?)\[\/url\]/is',
  12. '/\[url\](.*?)\[\/url\]/is',
  13. '/\[align\=(left|center|right)\](.*?)\[\/align\]/is',
  14. '/\[img\](.*?)\[\/img\]/is',
  15. '/\[mail\=(.*?)\](.*?)\[\/mail\]/is',
  16. '/\[mail\](.*?)\[\/mail\]/is',
  17. '/\[font\=(.*?)\](.*?)\[\/font\]/is',
  18. '/\[size\=(.*?)\](.*?)\[\/size\]/is',
  19. '/\[color\=(.*?)\](.*?)\[\/color\]/is',
  20. //added textarea for code presentation
  21. '/\[codearea\](.*?)\[\/codearea\]/is',
  22. //added pre class for code presentation
  23. '/\[code\](.*?)\[\/code\]/is',
  24. //added paragraph
  25. '/\[p\](.*?)\[\/p\]/is',
  26. );
  27.  
  28. $simple_replace = array(
  29. //added line break
  30. '<br />',
  31. '<strong>$1</strong>',
  32. '<em>$1</em>',
  33. '<u>$1</u>',
  34. // added nofollow to prevent spam
  35. '<a href="$1" rel="nofollow" title="$2 - $1" target="_blank">$2</a>',
  36. '<a href="$1" rel="nofollow" title="$1" target="_blank">$1</a>',
  37. '<div style="text-align: $1;">$2</div>',
  38. //added alt attribute for validation
  39. '<img src="$1" alt="" />',
  40. '<a href="mailto:$1" target="_blank">$2</a>',
  41. '<a href="mailto:$1" target="_blank">$1</a>',
  42. '<span style="font-family: $1;">$2</span>',
  43. '<span style="font-size: $1;">$2</span>',
  44. '<span style="color: $1;">$2</span>',
  45. //added textarea for code presentation
  46. '<textarea class="code_container" rows="30" cols="70">$1</textarea>',
  47. //added pre class for code presentation
  48. '<pre class="code">$1</pre>',
  49. //added paragraph
  50. '<p>$1</p>',
  51. );
  52.  
  53. // Do simple BBCode's
  54. $str = preg_replace ($simple_search, $simple_replace, $str);
  55.  
  56. // Do <blockquote> BBCode
  57. $str = bbcode_quote ($str);
  58.  
  59. return $str;
  60. }
  61.  
  62.  
  63.  
  64. function bbcode_quote ($str) {
  65. //added div and class for quotes
  66. $open = '<blockquote><div class="quote">';
  67. $close = '</div></blockquote>';
  68.  
  69. // How often is the open tag?
  70. preg_match_all ('/\[quote\]/i', $str, $matches);
  71. $opentags = count($matches['0']);
  72.  
  73. // How often is the close tag?
  74. preg_match_all ('/\[\/quote\]/i', $str, $matches);
  75. $closetags = count($matches['0']);
  76.  
  77. // Check how many tags have been unclosed
  78. // And add the unclosing tag at the end of the message
  79. $unclosed = $opentags - $closetags;
  80. for ($i = 0; $i < $unclosed; $i++) {
  81. $str .= '</div></blockquote>';
  82. }
  83.  
  84. // Do replacement
  85. $str = str_replace ('[' . 'quote]', $open, $str);
  86. $str = str_replace ('[/' . 'quote]', $close, $str);
  87.  
  88. return $str;
  89. }

Report this snippet  

You need to login to post a comment.