首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >调整BBcode解析器以记录URL中的解析笑脸?

调整BBcode解析器以记录URL中的解析笑脸?
EN

Stack Overflow用户
提问于 2013-02-05 20:46:55
回答 1查看 814关注 0票数 1

我想知道如何调整我的BBcode解析器,使其不解析URL中的笑脸?

下面是我的解析器:

代码语言:javascript
复制
    $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 );

这个问题出现的原因是有人输入了一个链接

代码语言:javascript
复制
http://pcgamingwiki.com/wiki/User:Soeb

然后尝试放入一个":S“笑脸图像?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-23 19:59:46

您可以使用preg_replace()代替str_replace(),并检查smiley是否由空格(或字符串的开头或结尾)限定

代码:

代码语言:javascript
复制
$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中查看它。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14707921

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档