php bbcode 与 mysql 检索

标签 php mysql bbcode

下面是我的 bbcode 数组中的示例代码。需要补充的我会在后面说明。

   $find = array(
    '~\<~s',
    '~\>~s',
    '~\[hr\]~s',
    '~\[b\](.*?)\[/b\]~s',
    '~\[i\](.*?)\[/i\]~s',
    '~\[u\](.*?)\[/u\]~s',
    '~\[s\](.*?)\[/s\]~s',
    '~\[ul\](.*?)\[/ul\]~s',
    '~\[li\](.*?)\[/li\]~s',
    '~\[ol\](.*?)\[/ol\]~s'
);
$replace = array(
    '&lt;',
    '&gt;',
    '<hr>',
    '<b>$1</b>',
    '<i>$1</i>',
    '<span style="text-decoration:underline;">$1</span>',
    '<del>$1</del>',
    '<ul>$1</ul>',
    '<li>$1</li>',
    '<ol>$1</ol>'
);
    $return = preg_replace($find, $replace, $text);
    return nl2br($return);

我需要做的是添加一个 [item] 标签,该标签将从 mysql 数据库中获取数据。

[项目]16[/项目] 将进入项目表并使用 id 16 获取名称和图像链接。然后显示:- 名称

我已经尝试这样做了一段时间,但走到了死胡同。任何建议都会很棒。谢谢。

回应@IllegalPigeon。

我能够修改您的代码,但我没有得到任何结果。我在我的主页上用一个大的查询来测试它。我已将其缩减为测试页面,运行了一个变量,但仍然无法获得任何结果。我正在使用 Mysqli 并且能够根据需要编辑查询。

我确信我走在正确的轨道上。可能只是遗漏了一些愚蠢的东西。

我当前的代码是:

工作代码。从@IllegalPigeon 回答更新。

<?php
//just db stuff.
include("config.php");
// BBcode array
$find = array(
    '~\<~s',
    '~\>~s',
    '~\[hr\]~s',
    '~\[b\](.*?)\[/b\]~s',
    '~\[i\](.*?)\[/i\]~s',
    '~\[u\](.*?)\[/u\]~s',
    '~\[s\](.*?)\[/s\]~s',
    '~\[ul\](.*?)\[/ul\]~s',
    '~\[li\](.*?)\[/li\]~s',
    '~\[ol\](.*?)\[/ol\]~s'
);
$replace = array(
    '&lt;',
    '&gt;',
    '<hr>',
    '<b>$1</b>',
    '<i>$1</i>',
    '<span style="text-decoration:underline;">$1</span>',
    '<del>$1</del>',
    '<ul>$1</ul>',
    '<li>$1</li>',
    '<ol>$1</ol>'
);

$text = "Test text.... [item]6[/item] .... text text";

preg_match_all('#\[item\](.*?)\[/item\]#i', $text, $matches, PREG_SET_ORDER );

for ( $i = 0, $j = count( $matches ); $i < $j; $i++ )
{
    $id = $matches[$i][1];

    if(filter_var($id, FILTER_VALIDATE_INT))
    {
        //It's a number, now you need to do your query
        //You didn't post most so modify your query to look like:
        $sql = "SELECT name, image_url FROM items WHERE id = $id";

        //Assuming you're using PDO, lets check if anything was returned
        if( $result = $db->query($sql) )
        {
        $row = $result->fetch_assoc();
            array_push($find, '~\[item\](.*?)\[/item\]~s');
            array_push($replace, '<img src="' . $row['image_url'] . '" title="' . $row['name'] . '" />');
        } else {
            die('There was an error running the query [' . $db->error . ']');
        }
    }
}
$return = preg_replace($find, $replace, $text);
echo  nl2br($return);

最佳答案

这完全未经测试,但是,我不明白为什么它不起作用。您将不得不自己进行一些编辑,因为我不知道您使用的是哪种数据库类(如果有的话)。

所以,试试这个代码:

    $find = array(
        '~\<~s',
        '~\>~s',
        '~\[hr\]~s',
        '~\[b\](.*?)\[/b\]~s',
        '~\[i\](.*?)\[/i\]~s',
        '~\[u\](.*?)\[/u\]~s',
        '~\[s\](.*?)\[/s\]~s',
        '~\[ul\](.*?)\[/ul\]~s',
        '~\[li\](.*?)\[/li\]~s',
        '~\[ol\](.*?)\[/ol\]~s'
    );
    $replace = array(
        '&lt;',
        '&gt;',
        '<hr>',
        '<b>$1</b>',
        '<i>$1</i>',
        '<span style="text-decoration:underline;">$1</span>',
        '<del>$1</del>',
        '<ul>$1</ul>',
        '<li>$1</li>',
        '<ol>$1</ol>'
    );
    preg_match_all('#\[item\](.*?)\[/item\]#i', $text, $matches, PREG_SET_ORDER );

    for ( $i = 0, $j = count( $matches ); $i < $j; $i++ )
    {
        $id = $matches[$i][1];

        //Lets make sure it is a number.

        if(filter_var($id, FILTER_VALIDATE_INT))
        {
            //It's a number, now you need to do your query
            //You didn't post most so modify your query to look like:
            //"SELECT name, image FROM yourTable WHERE id = $id"

            //Assuming you're using PDO, lets check if anything was returned
            if( $result = $con->fetch() ) 
            {
                array_push($find, '~\[item\](.*?)\[/item\]~s');
                array_push($replace, '<img src="' . $result['image'] . '" title="' . $result['name'] . '" />');
            }

        }   

    }
    $return = preg_replace($find, $replace, $text);
    return nl2br($return);

关于php bbcode 与 mysql 检索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33091099/

相关文章:

php - 使用 htaccess 隐藏 php GET

PHP 查看计数器垃圾邮件预防

php - 浏览器不读取解码的 html_entity_decode 文本

php - 如何统计每个用户发表的帖子总数

php - 具有 varchar id 的 Doctrine2 实体不将 id 插入数据库

mysql - 无法从 Glassfish 连接到 JDBC 连接池

jquery - 使用 jquery 的免费轻量级 bbcode 编辑器

javascript - 需要有关 phpBB 中 onclick BBCode Spoiler-Tag 的建议

mysql - 查找mysql中的行数

Javascript window.getSelection().focusNode.nodeValue 返回 HTML