php bbcode 与 mysql 检索
php bbcode with mysql retrieve
下面是我的 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(
'<',
'>',
'<hr>',
'<b></b>',
'<i></i>',
'<span style="text-decoration:underline;"></span>',
'<del></del>',
'<ul></ul>',
'<li></li>',
'<ol></ol>'
);
$return = preg_replace($find, $replace, $text);
return nl2br($return);
我需要做的是添加一个 [item] 标签,它将从 mysql 数据库中获取数据。
[item]16[/item]
将进入项目 table 并使用 ID 16 获取名称和图像 link。然后显示:- Name
我已经尝试这样做了一段时间,但我走到了死胡同。任何建议都会很棒。谢谢。
回应@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(
'<',
'>',
'<hr>',
'<b></b>',
'<i></i>',
'<span style="text-decoration:underline;"></span>',
'<del></del>',
'<ul></ul>',
'<li></li>',
'<ol></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);
这完全未经测试,但我不明白为什么它不起作用。您将不得不自己进行一些编辑,因为我不知道您使用的是哪种数据库 class,如果有的话。
所以,试试这个代码:
$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(
'<',
'>',
'<hr>',
'<b></b>',
'<i></i>',
'<span style="text-decoration:underline;"></span>',
'<del></del>',
'<ul></ul>',
'<li></li>',
'<ol></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);
下面是我的 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(
'<',
'>',
'<hr>',
'<b></b>',
'<i></i>',
'<span style="text-decoration:underline;"></span>',
'<del></del>',
'<ul></ul>',
'<li></li>',
'<ol></ol>'
);
$return = preg_replace($find, $replace, $text);
return nl2br($return);
我需要做的是添加一个 [item] 标签,它将从 mysql 数据库中获取数据。
[item]16[/item] 将进入项目 table 并使用 ID 16 获取名称和图像 link。然后显示:- Name
我已经尝试这样做了一段时间,但我走到了死胡同。任何建议都会很棒。谢谢。
回应@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(
'<',
'>',
'<hr>',
'<b></b>',
'<i></i>',
'<span style="text-decoration:underline;"></span>',
'<del></del>',
'<ul></ul>',
'<li></li>',
'<ol></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);
这完全未经测试,但我不明白为什么它不起作用。您将不得不自己进行一些编辑,因为我不知道您使用的是哪种数据库 class,如果有的话。
所以,试试这个代码:
$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(
'<',
'>',
'<hr>',
'<b></b>',
'<i></i>',
'<span style="text-decoration:underline;"></span>',
'<del></del>',
'<ul></ul>',
'<li></li>',
'<ol></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);