在我的表情符号代码中,它只替换了我的代码中的第一个,只表示:)可以显示图像。
我的代码中有什么问题。
我的代码
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).'';
答案 0 :(得分:3)
这是因为您在第一次替换后返回循环。将返回移动到循环外部。
foreach ($icons as $search => $replace) {
$text = preg_replace("#(?<=\s|^)" . preg_quote($search) . "#", $replace, $text);
}
return $text;