I made little javascript bbcode editor on my website. How to display that code from mysql? Or how to save in mysql as html that code?

function parseBBCode($text)
    $ALLOWABLE_TAGS = array("b", "i", "u"); //Add tags here
    static $PATTERNS = array();
    static $REPLACEMENTS = array();
    if (count($PATTERNS) == 0) {
        foreach ($ALLOWABLE_TAGS as $tag) {
            $PATTERNS[] = "/[$tag]/i";
            $PATTERNS[] = "/[\/$tag]/i";
            $REPLACEMENTS[] = "<$tag>";
            $REPLACEMENTS[] = "</$tag>";
    $result = str_replace(array(">", "<", "\"", "'"),
                       array("&gt;", "&lt;", "&quot;", "&#039;"),
    $result = preg_replace($PATTERNS, $REPLACEMENTS, $result);
    return $result;

Untested, but should work

Member Avatar

Looks good. How about having allowable bbcode tags set to different html tags? b -> strong, i -> em. That should make it more xhtml-compliant.

Thanks, but i have option for code like on this site. How to display php code on website i tried


but it doesn't work.

Member Avatar

This site uses '<pre class="code">' and ordered lists ('<ol><li>..</li>....</ol>') on every line to ensure line numbers.

How to transform every line to <li>?

Member Avatar

I assume it's:
Search for newline and replace with '</li><li>'. Place a '<li>' at the start and a '</li>' at the end.