我有一个textarea表单,支持BBcode,表单上写的文本使用PHP保存在我的数据库中,一切正常。
但是当我尝试从我的数据库打印数据时,我得到了这个:
并[b]你好[/ B]
而不是这个:
的你好
这是我从数据库打印的方式:
$sql = mysql_query("SELECT * FROM BlogData ORDER BY id DESC");
while($row = mysql_fetch_array($sql)){
$content = $row['content'];
echo nl2br($content);
我该怎么做才能看到我真正想要的文字?
答案 0 :(得分:0)
$regex = array(
'/\[b\](.*?)\[\/b\]/is' => '<b>$1</b>',
'/\[i\](.*?)\[\/i\]/is' => '<i>$1</i>',
'/\[u\](.*?)\[\/u\]/is' => '<u>$1</u>',
'/\[s\](.*?)\[\/s\]/is' => '<strike>$1</strike>',
'/\[sup\](.*?)\[\/sup\]/is' => '<sup>$1</sup>',
'/\[sub\](.*?)\[\/sub\]/is' => '<sub>$1</sub>',
'/\[color=#([0-9A-F]{6})\](.*?)\[\/color\]/is' => '<font color="#$1">$2</font>',
'/\[left\](.*?)\[\/left\]/is' => '<p style="text-align:left">$1</p>',
'/\[center\](.*?)\[\/center\]/is' => '<p style="text-align:center">$1</p>',
'/\[right\](.*?)\[\/right\]/is' => '<p style="text-align:right">$1</p>',
'/\[quote\](.*?)\[\/quote\]/is' => '<div class="quote"><span>$1</span></div>'
);
$sql = mysql_query("SELECT * FROM BlogData ORDER BY id DESC");
while($row = mysql_fetch_array($sql)){
$content = preg_replace(array_keys($regex), array_values($regex), $row['content']);
echo nl2br($content);
}