PHP replace all

2019-08-02 01:42发布

问题:

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

回答1:

Exactly the same way:

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


回答2:

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.



标签: php replace