Jump to content

bbcode and disabling html


Renlok

Recommended Posts

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("&lt;", "&gt;",
                "<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 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("&lt;", "&gt;",
                "<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 =]]
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]<?php

function 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]

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.