1

我想知道如何调整我的 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”笑脸图像?

4

1 回答 1

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 );

在这里查看它的实际应用。

于 2013-03-23T11:59:46.097 回答