php bbcode与mysql检索

php bbcode与mysql检索,php,mysql,bbcode,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\

下面是我的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获取名称和图像链接。然后显示:-名称

我试着这么做已经有一段时间了,但我已经走到了尽头。任何建议都很好。多谢各位

回应“非法鸽子”

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

我肯定我走对了方向。可能只是错过了一些愚蠢的事情

我目前的代码是: 工作代码。更新自@Illegalpege-answer

<?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);

对不起,打错了。它将显示:Image-Name*您可以尝试我的库来处理短代码:?如果你需要帮助,请告诉我。谢谢你的回复。在过去的一个小时里,我一直在玩这个游戏,但没有成功。我是新来的,不知道该如何回应。我用更多信息编辑了我的主要帖子。你的sql没有进行任何调用。您正在使用mysql\u查询吗?请检查站点的其他部分以获取查询,并向我展示它的外观。你是对的。对不起,我已经离开php一段时间了。我添加了$row=$result->fetch\u assoc;现在效果很好。谢谢你的帮助