bbCode,添加表情符号?

时间:2015-04-27 16:29:11

标签: php mysql pdo

所以我正在尝试将smileys添加到我的网站(bbCodes),但我无法弄清楚如何做到这一点。我在我的数据库中拥有所有的笑脸触发词和输出,以便更容易删除/添加表情符号。

下面这段代码什么也没做......我没有收到错误,也没有替换例如:happy:with image happy.png

error_reporting(E_ALL);
ini_set('display_errors', 1);
include('/var/www/files/connect.php');
$SmileysQ = $DB->query("SELECT * FROM smileys");
$SmileysQ->setFetchMode(PDO::FETCH_ASSOC);
while($Smileys = $SmileysQ->fetch()) {
$text = preg_replace ('/\''.$Smileys['trigger'].'/is', '<img src="images/smileys/'.$Smileys['output'].'.png" height="15" width="15" />', $text);
}

我做错了什么?

1 个答案:

答案 0 :(得分:1)

我认为preg_replace的第一个参数中有一个非预期的'标记,导致它在搜索':happy:时失败,而不是:happy:

适当的替换更可能是:

$text = preg_replace ('/'.$Smileys['trigger'].'/is', '<img src="images/smileys/'.$Smileys['output'].'.png" height="15" width="15" />', $text);

示例:

$text = ":happy: this is a test!";
$code = ":happy:";
$text = preg_replace ('/'.$code.'/is', '<img src="images/smileys/happy.png" height="15" width="15" />', $text);
print $text;
/*
outputs: <img src="images/smileys/happy.png" height="15" width="15" /> this is a test!
the extra ' gave me: :happy: this is a test!
*/