好吧,我会让添加笑脸变得更容易,并且应该更容易替换它的代码,所以创建 2 个数组,我只想有一个。所以这是我想要的一个例子。$Smileys = array(":D" => "");
所以不要像 $Smileys = array(":D"); 那样使用它。$SmileyReplace = 数组(“”);但有可能吗?我似乎在 Google 上找不到有用的东西。
我想使用的新代码和旧代码。新的:
function fixSmileys($Data) {
$Smileys = array(
":D" => '<img src="/application/modules/Chat/externals/images/smilies/grin.png" title=":D" alt=":D"/>',
":)" => '<img src="/application/modules/Chat/externals/images/smilies/smile.png" title=":)" alt=":)"/>',
":P" => '<img src="/application/modules/Chat/externals/images/smilies/tongue.png" title=":P" alt=":P"/>',
":S" => '<img src="/application/modules/Chat/externals/images/smilies/confused.png" title=":S" alt=":S"/>',
":'(" => '<img src="/application/modules/Chat/externals/images/smilies/cry.png" title=":'."'".'(" alt=":Cry:"/>',
":$" => '<img src="/application/modules/Chat/externals/images/smilies/embarrassed.png" title=":$" alt=":$"/>',
":(" => '<img src="/application/modules/Chat/externals/images/smilies/frown.png" title=":(" alt=":("/>',
":@" => '<img src="/application/modules/Chat/externals/images/smilies/mad.png" title=":@" alt=":@"/>',
";)" => '<img src="/application/modules/Chat/externals/images/smilies/wink.png" title=";)" alt=";)"/>',
"B)" => '<img src="/application/modules/Chat/externals/images/smilies/cool.png" title="B)" alt="B)"/>',
":|" => '<img src="/application/modules/Chat/externals/images/smilies/neutral.png" title=":|" alt=":|"/>',
":lol:" => '( ?° ?? ?°)',
":derp:" => '<img src="/application/modules/Chat/externals/images/smilies/derp.png" title=":derp:" alt=":derp:"/>',
";D" => '<img src="/application/modules/Chat/externals/images/smilies/awesome.png" title=";D" alt=";D"/>',
":troll:" => '<img src="/application/modules/Chat/externals/images/smilies/troll.png" title=":troll:" alt=":troll:"/>',
":spin:" => '<img src="/application/modules/Chat/externals/images/smilies/abspin.gif" title=":spin:" alt=":spin:"/>',
":love:" => '<img src="/application/modules/Chat/externals/images/smilies/heart.png" title=":love:" alt=":love:"/>',
":sick:" => '<img src="/application/modules/Chat/externals/images/smilies/sick.png" title=":lol:" alt=":sick:"/>',
":O_O:" => '<img src="/application/modules/Chat/externals/images/smilies/sawut.png" title=":O_O:" alt=":O_O:"/>',
":bath:" => '<img src="/application/modules/Chat/externals/images/smilies/bath-time.png" title=":bath:" alt=":bath:"/>',
":socks:" => '<img src="/application/modules/Chat/externals/images/smilies/socks.png" title=":socks:" alt=":socks:"/>',
":boss:" => '<img src="/application/modules/Chat/externals/images/smilies/SuitBozzsmiley.png" title=":boss:" alt=":boss:"/>',
":potato:" => '<img src="/application/modules/Chat/externals/images/smilies/potatoes.png" title=":potato:" alt=":potato:"/>');
return $Smileys[$Data];
}
而我目前使用的旧代码仅使用 str_replace 和很多行。
function fixSmileys($Data) {
$xCommentx = $Data;
$xCommentx1 = str_replace(":D", '<img src="/application/modules/Chat/externals/images/smilies/grin.png" title=":D" alt=":D"/>', $xCommentx);
$xCommentx2 = str_replace(":)", '<img src="/application/modules/Chat/externals/images/smilies/smile.png" title=":)" alt=":)"/>', $xCommentx1);
$xCommentx3 = str_replace(":P", '<img src="/application/modules/Chat/externals/images/smilies/tongue.png" title=":P" alt=":P"/>', $xCommentx2);
$xCommentx4 = str_replace(":S", '<img src="/application/modules/Chat/externals/images/smilies/confused.png" title=":S" alt=":S"/>', $xCommentx3);
$xCommentx5 = str_replace(":'(", '<img src="/application/modules/Chat/externals/images/smilies/cry.png" title=":'."'".'(" alt=":Cry:"/>', $xCommentx4);
$xCommentx6 = str_replace(":$", '<img src="/application/modules/Chat/externals/images/smilies/embarrassed.png" title=":$" alt=":$"/>', $xCommentx5);
$xCommentx7 = str_replace(":(", '<img src="/application/modules/Chat/externals/images/smilies/frown.png" title=":(" alt=":("/>', $xCommentx6);
$xCommentx8 = str_replace(":@", '<img src="/application/modules/Chat/externals/images/smilies/mad.png" title=":@" alt=":@"/>', $xCommentx7);
$xCommentx9 = str_replace(";)", '<img src="/application/modules/Chat/externals/images/smilies/wink.png" title=";)" alt=";)"/>', $xCommentx8);
$xCommentx10 = str_replace("B)", '<img src="/application/modules/Chat/externals/images/smilies/cool.png" title="B)" alt="B)"/>', $xCommentx9);
$xCommentx11 = str_replace(":lol:", '( ͡° ͜ʖ ͡°)', $xCommentx10);
$xCommentx12 = str_replace(":derp:", '<img src="/application/modules/Chat/externals/images/smilies/derp.png" title=":derp:" alt=":derp:"/>', $xCommentx11);
$xCommentx13 = str_replace(";D", '<img src="/application/modules/Chat/externals/images/smilies/awesome.png" title=";D" alt=";D"/>', $xCommentx12);
$xCommentx14 = str_replace(":troll:", '<img src="/application/modules/Chat/externals/images/smilies/troll.png" title=":troll:" alt=":troll:"/>', $xCommentx13);
$xCommentx15 = str_replace(":approve:", '<img src="/application/modules/Chat/externals/images/smilies/approved.png" title=":approve:" alt=":approve:"/>', $xCommentx14);
$xCommentx16 = str_replace(":lolol:", '<img src="/application/modules/Chat/externals/images/smilies/lolol.png" title=":brohoof:" alt=":brohoof:"/>', $xCommentx15);
$xCommentx17 = str_replace(":asdf:", '<img src="/application/modules/Chat/externals/images/smilies/asdf.png" title=":facehoof:" alt=":facehoof:"/>', $xCommentx16);
$xCommentx18 = str_replace(":spin:", '<img src="/application/modules/Chat/externals/images/smilies/abspin.gif" title=":spin:" alt=":spin:"/>', $xCommentx17);
$xCommentx19 = str_replace(":love:", '<img src="/application/modules/Chat/externals/images/smilies/heart.png" title=":love:" alt=":love:"/>', $xCommentx18);
$xCommentx20 = str_replace(":sick:", '<img src="/application/modules/Chat/externals/images/smilies/sick.png" title=":lol:" alt=":sick:"/>', $xCommentx19);
$xCommentx21 = str_replace(":O_O:", '<img src="/application/modules/Chat/externals/images/smilies/sawut.png" title=":O_O:" alt=":O_O:"/>', $xCommentx20);
$xCommentx22 = str_replace(":bath:", '<img src="/application/modules/Chat/externals/images/smilies/bath-time.png" title=":bath:" alt=":bath:"/>', $xCommentx21);
$xCommentx23 = str_replace(":socks:", '<img src="/application/modules/Chat/externals/images/smilies/socks.png" title=":socks:" alt=":socks:"/>', $xCommentx22);
$xCommentx24 = str_replace(":boss:", '<img src="/application/modules/Chat/externals/images/smilies/SuitBozzsmiley.png" title=":boss:" alt=":boss:"/>', $xCommentx23);
$xCommentx25 = str_replace(":potato:", '<img src="/application/modules/Chat/externals/images/smilies/potatoes.png" title=":potato:" alt=":potato:"/>', $xCommentx24);
$xCommentxFinal = str_replace(":|", '<img src="/application/modules/Chat/externals/images/smilies/neutral.png" title=":|" alt=":|"/>', $xCommentx25);
return $xCommentxFinal;
}
当前代码的工作方式如下,正在馈送的 $Data 包含类似“Hello world:D”,因此它会遍历所有 str_replaces,直到找到匹配项并将其替换为 html 代码。但我只想让它像新的但无用的非工作代码一样。我想要这样的原因是更容易将笑脸添加到网站。