【问题标题】:Replace bbcode with html tags用html标签替换bbcode
【发布时间】:2014-11-24 10:32:10
【问题描述】:

我正在编写一个用通讯员 html 标记替换 bbcodes 的小脚本。 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>';

【问题讨论】:

  • 它确实返回了某种错误?
  • 不,它只是返回输入字符串,例如[b]foo[/b]
  • preg_replace_callback 的文档页面有一个带有 bbcode 功能的用户评论。也许你可以看看那个“灵感”。 ;)

标签: php replace bbcode


【解决方案1】:
$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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多