PHP replace all

2019-08-02 01:57发布

I have this in JavaScript:

msg = msg.replace(/(:\)|=\)|:-\)|\(:)/gi, "<img src='img/ei/1.png' class='ei' />");

Is there a similar way I can do that, but in PHP?

Thanks in advance, enji

标签: php replace
2条回答
女痞
2楼-- · 2019-08-02 02:18

Exactly the same way:

$msg = preg_replace('/(:\)|=\)|:-\)|\(:)/i', "<img src='img/ei/1.png' class='ei' />", $msg);
查看更多
劳资没心,怎么记你
3楼-- · 2019-08-02 02:20

If i understand correctly you are trying to replace instances of the :\ smiley with an image. You could do something like this:

<?php
    $str = "Hey there :)";

    str_replace(
      array(":)", "=)", ":-)", "(:"), 
      "<img src='img/ei/1.png' class='ei' />", 
      $str);
?>

Shai.

查看更多
登录 后发表回答