在我的表情符号代码中,它只替换了我的第一个代码,这意味着只有:)可以显示图像。
请问我的代码中有什么错误。
我的代码
function smileys($text){
// Smiley to image
$icons = array(
':)' => '<img src="smilies/smile.gif" border="0" alt="" />',
':-)' => '<img src="smilies/smile.gif" border="0" alt="" />',
':D' => '<img src="smilies/smile.gif" border="0" alt="" />',
':-(' => '<img src="smilies/angry.gif" border="0" alt="" />',
'angel' => '<img src="smilies/angel.gif" border="0" alt="" />',
'at' => '<img src="smilies/at.gif" border="0" alt="" />',
':-D' => '<img src="smilies/biggrin.gif" border="0" alt="" />',
'lol' => '<img src="smilies/biggrin.gif" border="0" alt="" />',
':-]' => '<img src="smilies/blush.gif" border="0" alt="" />',
':-?' => '<img src="smilies/confused.gif" border="0" alt="" />',
'B-)' => '<img src="smilies/cool.gif" border="0" alt="" />',
'B)' => '<img src="smilies/cool.gif" border="0" alt="" />',
';)' => '<img src="smilies/dodgy.gif" border="0" alt="" />',
':(' => '<img src="smilies/sad.gif" border="0" alt="" />',
':-(' => '<img src="smilies/sad.gif" border="0" alt="" />',
'shy' => '<img src="smilies/shy.gif" border="0" alt="" />',
'|-)' => '<img src="smilies/sleepy.gif" border="0" alt="" />',
':-P' => '<img src="smilies/tongue.gif" border="0" alt="" />',
':-|' => '<img src="smilies/undecided.gif" border="0" alt="" />',
';-)' => '<img src="smilies/wink.gif" border="0" alt="" />',
);
// Now you need find and replace
foreach($icons as $search => $replace){
$text = preg_replace("#(?<='s|^)" . preg_quote($search) . "#", $replace, $text);
return $text;
}
}
echo''.smileys($description).'';
这是因为您在第一次替换后返回循环。将回波移出循环。
foreach ($icons as $search => $replace) {
$text = preg_replace("#(?<='s|^)" . preg_quote($search) . "#", $replace, $text);
}
return $text;