Replace bbcode with html tags

637 views Asked by At

I'm writing a ittle script that replace bbcodes with correspondents html tags. Both bbcodes and html tags are stored in a database. This is my actual code for [b][/b], but it doesn't works

function bbcode($matches)
{
$conn = mysqli_connect('127.0.0.1','root','','esame') or die("Connection failed: " . $conn->connect_error);
foreach ($matches as $match)
    {
    $query = mysqli_query($conn,"SELECT html FROM `bbcode` WHERE bbcode='{$match}' ");
        while ($html = mysqli_fetch_array($query,MYSQLI_ASSOC))
        {
        return 'Html: '.$html['html'];
        }
    $conn->close();
    }
}

$regex = '/\[b\](.+)\[\/b\]/is';
echo '<br>'.preg_replace_callback($regex,"bbcode",$text).'<br>';
0

There are 0 answers