Extended PHP BBCode Parse


/ Published in: PHP
Save to your folder(s)

Extended BBCode parsing.


Copy this code and paste it in your HTML
  1. function bbcode_format($str) {
  2. $str = htmlentities($str);
  3.  
  4. $simple_search = array(
  5. '/\[b\](.*?)\[\/b\]/is',
  6. '/\[i\](.*?)\[\/i\]/is',
  7. '/\[u\](.*?)\[\/u\]/is',
  8. '/\[url\=(.*?)\](.*?)\[\/url\]/is',
  9. '/\[url\](.*?)\[\/url\]/is',
  10. '/\[align\=(left|center|right)\](.*?)\[\/align\]/is',
  11. '/\[img\](.*?)\[\/img\]/is',
  12. '/\[mail\=(.*?)\](.*?)\[\/mail\]/is',
  13. '/\[mail\](.*?)\[\/mail\]/is',
  14. '/\[font\=(.*?)\](.*?)\[\/font\]/is',
  15. '/\[size\=(.*?)\](.*?)\[\/size\]/is',
  16. '/\[color\=(.*?)\](.*?)\[\/color\]/is',
  17. '/\[thumb\=(.*?)\](.*?)\[\/thumb\]/is',
  18. '/\[strike\](.*?)\[\/strike\]/is',
  19. '/\[br\]/is',
  20. '/\[imglink\=(.*?)\](.*?)\[\/imglink\]/is',
  21. );
  22.  
  23. $simple_replace = array(
  24. '<strong>$1</strong>',
  25. '<em>$1</em>',
  26. '<u>$1</u>',
  27. '<a href="$1" target="_blank">$2</a>'.
  28. '<a href="$1" target="_blank">$1</a>',
  29. '<div style="text-align: $1;">$2</div>',
  30. '<img src="$1" alt="Picture" border="0"/>',
  31. '<a href="mailto:$1">$2</a>',
  32. '<a href="mailto:$1">$1</a>',
  33. '<span style="font-family: $1;">$2</span>',
  34. '<span style="font-size: $1;">$2</span>',
  35. '<span style="color: $1;">$2</span>',
  36. '<img width="$1" src="$2" alt="Picture" border="0"/>',
  37. '<span style="text-decoration: line-through;">$1</span>',
  38. '<br />',
  39. '<a href="$1"><img src="$2" alt="Picture" border="0" /></a>',
  40. );
  41.  
  42. // Do simple BBCode's
  43. $str = preg_replace ($simple_search, $simple_replace, $str);
  44.  
  45. return $str;
  46. }

URL: http://underscoreundefined.co.cc

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.