所以我试着把笑脸添加到我的网站(bbCodes)中,但是我不知道该怎么做。我的数据库中有所有的笑脸触发器和输出,以便更容易地删除/添加笑脸。
下面这段代码什么也做不了..。我没有得到一个错误,并且它没有替换,例如:高兴:使用图像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);
}我做错了什么?
发布于 2015-04-27 16:34:35
我认为您在'的第一个参数中有一个意外的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!
*/https://stackoverflow.com/questions/29901214
复制相似问题