Renlok Posted January 19, 2007 Share Posted January 19, 2007 well ive made a forum, and i was wondering if someone could show me how to make a simple piece of bbcode such as [code][b][/b][/code] and how to disable html within the posts.thanks for anyhelp :) Link to comment https://forums.phpfreaks.com/topic/34913-bbcode-and-disabling-html/ Share on other sites More sharing options...
owenjh Posted January 19, 2007 Share Posted January 19, 2007 Disabling HTML is as easy as using htmlspecialchars when storing the post.To do bbcode you can use this:[CODE]function bb2html($text){ $bbcode = array("<", ">", "[list]", "[*]", "[/list]", "[img]", "[/img]", "[b]", "[/b]", "[u]", "[/u]", "[i]", "[/i]", '[color="', "[/color]", "[size=\"", "[/size]", '[url="', "[/url]", "[mail=\"", "[/mail]", "[ code]", "[ /code]", "[ quote]", "[ /quote]", '"]'); $htmlcode = array("<", ">", "<ul>", "<li>", "</ul>", "<img src=\"", "\">", "<b>", "</b>", "<u>", "</u>", "<i>", "</i>", "<span style=\"color:", "</span>", "<span style=\"font-size:", "</span>", '<a href="', "</a>", "<a href=\"mailto:", "</a>", "<code>", "</code>", "<table width=100% bgcolor=lightgray><tr><td bgcolor=white>", "</td></tr></table>", '">'); $newtext = str_replace($bbcode, $htmlcode, $text); $newtext = nl2br($newtext);//second pass return $newtext;}[/CODE] Link to comment https://forums.phpfreaks.com/topic/34913-bbcode-and-disabling-html/#findComment-164646 Share on other sites More sharing options...
Renlok Posted January 20, 2007 Author Share Posted January 20, 2007 [quote author=owenjh link=topic=123171.msg508792#msg508792 date=1169239173]Disabling HTML is as easy as using htmlspecialchars when storing the post.To do bbcode you can use this:[CODE]function bb2html($text){ $bbcode = array("<", ">", "[list]", "[*]", "[/list]", "[img]", "[/img]", "[b]", "[/b]", "[u]", "[/u]", "[i]", "[/i]", '[color="', "[/color]", "[size=\"", "[/size]", '[url="', "[/url]", "[mail=\"", "[/mail]", "[ code]", "[ /code]", "[quote ]", "[ /quote]", '"]'); $htmlcode = array("<", ">", "<ul>", "<li>", "</ul>", "<img src=\"", "\">", "<b>", "</b>", "<u>", "</u>", "<i>", "</i>", "<span style=\"color:", "</span>", "<span style=\"font-size:", "</span>", '<a href="', "</a>", "<a href=\"mailto:", "</a>", "<code>", "</code>", "<table width=100% bgcolor=lightgray><tr><td bgcolor=white>", "</td></tr></table>", '">'); $newtext = str_replace($bbcode, $htmlcode, $text); $newtext = nl2br($newtext);//second pass return $newtext;}[/CODE][/quote]thanks so you just run that before soting the post =]] Link to comment https://forums.phpfreaks.com/topic/34913-bbcode-and-disabling-html/#findComment-164955 Share on other sites More sharing options...
wildteen88 Posted January 20, 2007 Share Posted January 20, 2007 When dealing with bbcode it is best to you use regex (preg_replace) rather than str_replace. str_replace is only for doing basic string replacements. If you look in the regex board you will see plenty of examples. Here is a very basic example:[code]<?phpfunction bbcode($txt){ // bbcodes $bbcodes = array( "|\[b\](.+?)\[/b\]|is", "|\[u\](.+?)\[/u\]|is", "|\[i\](.+?)\[/i\]|is" ); // html $replace = array( "<strong>$1</strong>", "<u>$1</u>", "<em>$1</em>" ); $txt = preg_replace($bbcodes, $replace, $txt); return nl2br($txt);}$str = "[b]hey[/b] a [u][i]BBCode parser[/i][/u]!";$str = bbcode($str);echo $str;?>[/code] Link to comment https://forums.phpfreaks.com/topic/34913-bbcode-and-disabling-html/#findComment-165053 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.