调整BBcode解析器以记录URL中的解析表情符号?

时间:2013-02-05 12:46:55

标签: php regex bbcode

我想知道如何调整我的BBcode解析器以解析URL中的表情符号?

这是我的解析器:

    $smilies = array(   
"><" => '<img src="/jscripts/sce/emoticons/angry.png" alt="" />',
":'(" => '<img src="/jscripts/sce/emoticons/cry.png" alt="" />',
":S" => '<img src="/jscripts/sce/emoticons/dizzy.png" alt="" />',
":D" => '<img src="/jscripts/sce/emoticons/grin.png" alt="" />',
"^_^" => '<img src="/jscripts/sce/emoticons/happy.png" alt="" />',
"<3" => '<img src="/jscripts/sce/emoticons/heart.png" alt="" />',
":huh:" => '<img src="/jscripts/sce/emoticons/huh.png" alt="" />',
":|" => '<img src="/jscripts/sce/emoticons/pouty.png" alt="" />',
":(" => '<img src="/jscripts/sce/emoticons/sad.png" alt=""/>',
":O" => '<img src="/jscripts/sce/emoticons/shocked.png" alt="" />',
":sick:" => '<img src="/jscripts/sce/emoticons/sick.png" alt="" />',
":)" => '<img src="/jscripts/sce/emoticons/smile.png" alt="" />',
":P" => '<img src="/jscripts/sce/emoticons/tongue.png" alt="" />',
":S" => '<img src="/jscripts/sce/emoticons/unsure.png" alt="" />',
":woot:" => '<img src="/jscripts/sce/emoticons/w00t.png" alt="" />',
":whistle:" => '<img src="/jscripts/sce/emoticons/whistle.png" alt="" />',
";)" => '<img src="/jscripts/sce/emoticons/wink.png" alt="" />',
":wub:" => '<img src="/jscripts/sce/emoticons/wub.png" alt="" />'
);

$body = str_replace( array_keys( $smilies ), array_values( $smilies ), $body );

当有人插入

链接时,问题就出现了
http://pcgamingwiki.com/wiki/User:Soeb

然后试图放入“:S”笑脸图片?

1 个答案:

答案 0 :(得分:2)

您可以使用preg_replace()代替str_replace()并检查笑脸是否受空格(或字符串的开头或字符串结尾)的限制

代码:

$smilies = array(   
"/( |^)><( |$)/" => ' <img src="/jscripts/sce/emoticons/angry.png" alt="" /> ',
"/( |^):'\(( |$)/" => ' <img src="/jscripts/sce/emoticons/cry.png" alt="" /> ',
"/( |^):S( |$)/" => ' <img src="/jscripts/sce/emoticons/dizzy.png" alt="" /> ',
"/( |^):D( |$)/" => ' <img src="/jscripts/sce/emoticons/grin.png" alt="" /> ',
"/( |^)\^_\^( |$)/" => ' <img src="/jscripts/sce/emoticons/happy.png" alt="" /> ',
"/( |^)<3( |$)/" => ' <img src="/jscripts/sce/emoticons/heart.png" alt="" /> ',
"/( |^):huh:( |$)/" => ' <img src="/jscripts/sce/emoticons/huh.png" alt="" /> ',
"/( |^):\|( |$)/" => ' <img src="/jscripts/sce/emoticons/pouty.png" alt="" /> ',
"/( |^):\(( |$)/" => ' <img src="/jscripts/sce/emoticons/sad.png" alt=""/> ',
"/( |^):O( |$)/" => ' <img src="/jscripts/sce/emoticons/shocked.png" alt="" /> ',
"/( |^):sick:( |$)/" => ' <img src="/jscripts/sce/emoticons/sick.png" alt="" /> ',
"/( |^):\)( |$)/" => ' <img src="/jscripts/sce/emoticons/smile.png" alt="" /> ',
"/( |^):P( |$)/" => ' <img src="/jscripts/sce/emoticons/tongue.png" alt="" /> ',
"/( |^):S( |$)/" => ' <img src="/jscripts/sce/emoticons/unsure.png" alt="" /> ',
"/( |^):woot:( |$)/" => ' <img src="/jscripts/sce/emoticons/w00t.png" alt="" /> ',
"/( |^):whistle:( |$)/" => ' <img src="/jscripts/sce/emoticons/whistle.png" alt="" /> ',
"/( |^);\)( |$)/" => ' <img src="/jscripts/sce/emoticons/wink.png" alt="" /> ',
"/( |^):wub:( |$)/" => ' <img src="/jscripts/sce/emoticons/wub.png" alt="" /> '
);

$body=preg_replace( array_keys($smilies), array_values($smilies), $body );

在行动here中查看。