用html标签替换bbcode

时间:2014-11-24 10:32:11

标签: php replace bbcode

我正在写一个用通讯录html标签替换bbcodes的ittle脚本。 bbcodes和html标签都存储在数据库中。这是我[b] [/ b]的实际代码,但它不起作用

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>';

1 个答案:

答案 0 :(得分:0)

$str = "[list=1]\n[*]Камиль [/*]\n[*]Хисматуллин [/*]\n[*]живет в Урюпинске [/*]\n[/list]"; 

$advanced_bbcode = array(
    '/(?si)\\[list=\\d+\\](.*?)\\[\\/list\\]/',
    '/(?si)\\[\\*\\](.*?)\\[\\/\\*\\]/'
);

$advanced_html = array(
    '<ol>$1</ol>',
    '<li>$1</li>'
);

$text = preg_replace($advanced_bbcode, $advanced_html, $str);
echo $text;