Jump to content

Recommended Posts

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.