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?

Recommended Answers

All 6 Replies

<?php
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;"),
                       $text);
    $result = preg_replace($PATTERNS, $REPLACEMENTS, $result);
    return $result;
}
?>

Untested, but should work

Member Avatar for diafol

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

<pre>

but it doesn't work.

Member Avatar for diafol

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 for diafol

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.