用str_replace替换带有数组的文本

时间:2012-12-22 00:46:19

标签: php str-replace

好吧,我会更容易添加表情符号和应该更容易替换的代码,因此创建了2个数组,我想只有一个。 所以这是我想要的一个例子。 $ Smileys = array(“:D”=>“”);

所以不要像使用它那样 $ Smileys = array(“:D”); $ SmileyReplace = array(“”);  但它有可能吗? 我似乎无法在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代码替换它。 但我想让它像新的,但无用的非工作代码。 我希望这样做的原因是让向网站添加表情符号更容易。

3 个答案:

答案 0 :(得分:5)

有一种使用strtr执行此操作的简单方法。请参阅文档和示例http://php.net/manual/en/function.strtr.php

答案 1 :(得分:3)

您可以尝试在一个函数中使用它,方法是使用buildin函数将数组拆分为两个,例如:

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

编辑:请记住,这些函数会复制数组,因此有一个更好的解决方案,而不是将一个数组拆分为两个;)

答案 2 :(得分: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);