Posted By

ZaiSL on 01/08/11


Tagged


Versions (?)

BB Code to HTML


 / Published in: PHP
 

Simply call the function by passing your string you wish to parse. To convert the parser to HTML to BB Code, change this line:

$newtext = strreplace($bbcode, $htmlcode, $text); to $newtext = strreplace($htmlcode, $bbcode, $text);

  1. function bb2html($text)
  2. {
  3. $bbcode = array("<", ">",
  4. "[list]", "[*]", "[/list]",
  5. "[img]", "[/img]",
  6. "[b]", "[/b]",
  7. "[u]", "[/u]",
  8. "[i]", "[/i]",
  9. '[color="', "[/color]",
  10. "[size="", "[/size]",
  11. '[url="', "[/url]",
  12. "[mail="", "[/mail]",
  13. "[code]", "[/code]",
  14. "[quote]", "[/quote]",
  15. '"]');
  16. $htmlcode = array("&lt;", "&gt;",
  17. "<ul>", "<li>", "</ul>",
  18. "<img src="", "">",
  19. "<b>", "</b>",
  20. "<u>", "</u>",
  21. "<i>", "</i>",
  22. "<span style="color:", "</span>",
  23. "<span style="font-size:", "</span>",
  24. '<a href="', "</a>",
  25. "<a href="mailto:", "</a>",
  26. "<code>", "</code>",
  27. "<table width=100% bgcolor=lightgray><tr><td bgcolor=white>", "</td></tr></table>",
  28. '">');
  29. $newtext = str_replace($bbcode, $htmlcode, $text);
  30. $newtext = nl2br($newtext);//second pass
  31. return $newtext;
  32. }

Report this snippet  

You need to login to post a comment.