0

好吧,我会让添加笑脸变得更容易,并且应该更容易替换它的代码,所以创建 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 代码。但我只想让它像新的但无用的非工作代码一样。我想要这样的原因是更容易将笑脸添加到网站。

4

3 回答 3

5

有一种简单的方法可以使用strtr. 请参阅文档和示例http://php.net/manual/en/function.strtr.php

于 2012-12-22T01:06:07.447 回答
3

您可以通过使用内置函数将数组分成两个来尝试在一个函数中使用它,例如:

...
return str_replace(array_keys($Smileys), array_values($Smileys), $Data)

编辑:请记住,这些函数会复制数组,因此可能有比将一个数组分成两个更好的解决方案;)

于 2012-12-22T00:55:43.597 回答
0

我希望这可以帮助你。:)

function fixSmileys($Data,$direction) {
        $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=":)"/>');

    foreach($Smileys as $key=>$value){//echo $key;
      if($direction==1){$Data=str_replace($key,$value,$Data);}
      elseif($direction==2){$Data=str_replace($value,$key,$Data);}
    }

        return $Data;
}

并像这样使用它:

echo fixSmileys("hello there :D",1)."<br>".fixSmileys('<img src="/application/modules/Chat/externals/images/smilies/grin.png" title=":D" alt=":D"/>',2);
于 2012-12-22T00:58:53.553 回答