bbCode,添加笑脸?
bbCode, adding smileys?
所以我想在我的网站 (bbCodes) 中添加笑脸符号,但我不知道该怎么做。我的数据库中有所有笑脸触发词和输出,以便 remove/add 笑脸更容易。
下面这段代码什么都不做...我没有收到错误,它也没有用图像替换例如 :happy: 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);
}
我做错了什么?
我认为您在 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!
*/
所以我想在我的网站 (bbCodes) 中添加笑脸符号,但我不知道该怎么做。我的数据库中有所有笑脸触发词和输出,以便 remove/add 笑脸更容易。
下面这段代码什么都不做...我没有收到错误,它也没有用图像替换例如 :happy: 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);
}
我做错了什么?
我认为您在 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!
*/