Clarkeez Posted August 8, 2011 Share Posted August 8, 2011 Hey This is the current function I am using to parse bbcode to their respective html tags from a string. The problem is, if the given string has for example they forget to put the ending tag [/b] after they have used then the rest of the page is bold. How can I stop this? <?php function bbcode($input) { $bbcode = array('[b]', '[/b]', '[i]', '[/i]', '[u]', '[/u]', ' [center]', '[/center] '); $htcode = array('<b>', '</b>', '<i>', '</i>', '<u>', '</u>', '<center>', '</center>'); return str_replace($bbcode, $htcode, $input); } ?> HTML <div class="post"> <?php echo bbcode($sql_data['post']); ?> <!-- <This guy forgets to end his bold tag --> </div> <div class="post"> <?php echo bbcode($sql_data['post']); ?> <!-- <This guys post is now bold --> </div> Link to comment https://forums.phpfreaks.com/topic/244261-bbcode-parser/ Share on other sites More sharing options...
jcbones Posted August 8, 2011 Share Posted August 8, 2011 When I run bbcode, I require that both opening and closing brackets are in place, otherwise the bracket will just be printed out. function bbcode($content) { $content = preg_replace("~\[b\](.+?)\[\/b\]~i", "<b>$1</b>", $content); $content = preg_replace("~\[i\](.+?)\[\/i\]~i", "<i>$1</i>", $content); $content = preg_replace("~\[u\](.+?)\[\/u\]~i", "<u>$1</u>", $content); $content = preg_replace("~\[img\](.+?)\[\/img\]~i", "<img src=\"$1\" alt=\"\" />", $content); $content = preg_replace("~\[url\](.+?)\[\/url\]~i", "<a href=\"$1\">[Link]</a>", $content); $content = preg_replace("~\[url=http://(.+?)\](.+?)\[\/url\]~i", "<a href=\"$1\">$2</a>", $content); $content = preg_replace("~\[code\](.+?)\[\/code\]~i", "<div class=\"box\">$1</div>", $content); return($content); } Link to comment https://forums.phpfreaks.com/topic/244261-bbcode-parser/#findComment-1254563 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.