A better way to replace emoticons in PHP?

后端 未结 5 1540
忘掉有多难
忘掉有多难 2020-12-05 21:40

Right now I am using this function for emoticons:

function emoticons($text) {
        $icons = array(
                \':)\'    =>  \'

        
5条回答
  •  臣服心动
    2020-12-05 22:25

    You can use the preg_replace function and then use word boundaries in the regular expression.

     foreach($icons as $icon=>$image) {
          $icon = preg_quote($icon);
          $text = preg_replace("~\b$icon\b~",$image,$text);
     }
    

    You need to use word boundaries and not white space because this will take care of the start and end points to. Needing a space before means that just a :) won't be found.

提交回复
热议问题