php用图释图像替换字符串。问题与mysql表情符号数据形成一个数组

时间:2015-09-25 06:12:39

标签: php mysql

我试图在字符串$ textmchat中替换任何表情符号纯文本符号(例如:: P :):P:hello :: goodbye :)以及存储在我的MYSQL数据库中的相应表情符号数据(是的,我知道我应该使用PDO)。我得到了:注意:未定义索引:代码,情感,smiley_url,smiley_width和smiley_height,代码如下所示。我不知道什么是错的,任何帮助都会受到赞赏。 MYSQL查询中的SELECT数据确实存在,当然不是null。

// example text that needs to be replaced with smilies

$textmchat = 'lorem ipsum :) :) lorem ipsum :P lorem';

mysql_connect("$dbhost", "$dbuser", "$dbpasswd") or die(mysql_error());
mysql_select_db("$dbname") or die(mysql_error());

$result = mysql_query("SELECT code, emotion, smiley_url, smiley_width, smiley_height FROM phpbb_smilies WHERE display_on_posting = '1'")
or die(mysql_error());
    $smilies = array();

while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    $smilies[$row['code']] = array($row['emotion'],  $row['smiley_url'],  $row['smiley_width'],  $row['smiley_height'] );

}
foreach($smilies as $key => $img) {
    $textmchat = str_replace($key, '<img src="images/smilies/'.$img['smiley_url'].'" alt="'.$img['emotion'].'" title="'.$img['emotion'].'" height="'.$img['smiley_height'].'" width="'.$img['smiley_width'].'" />', $textmchat);
}

1 个答案:

答案 0 :(得分:1)

试试这个:

// example text that needs to be replaced with smilies

$textmchat = 'dog cat fish LOL  :) :) jump duck :P dive';

mysql_connect("$dbhost", "$dbuser", "$dbpasswd") or die(mysql_error());
mysql_select_db("$dbname") or die(mysql_error());

$result = mysql_query("SELECT code, emotion, smiley_url, smiley_width, smiley_height FROM phpbb_smilies WHERE display_on_posting = '1'")
or die(mysql_error());
while($row = mysql_fetch_array($result, MYSQL_ASSOC){
    $smilies[$row['code']] = array($row['emotion'],  $row['smiley_url'],  $row['smiley_width'],  $row['smiley_height'] );
}
foreach($smiles as $key => $img) {
    $textmchat = str_replace($key, '<img src="images/smilies/'.$img['smiley_url'].'" alt="'.$img['emotion'].'" title="'.$img['emotion'].'" height="'.$img['smiley_height'].'"     width="'.$img['smiley_width'].'" />', $textmchat);
}