Match and replace emoticons in string - what is the most efficient way?

前端 未结 5 1358
庸人自扰
庸人自扰 2020-12-11 23:03

Wikipedia defines a lot of possible emoticons people can use. I want to match this list to words in a string. I now have this:

$string = \"Lorem ipsum :-) do         


        
5条回答
  •  独厮守ぢ
    2020-12-11 23:41

    If the $string, in which you want replace emoticons, is provided by a visitor of your site(I mean it's a user's input like comment or something), then you should not relay that there will be a space before or after the emoticon. Also there are at least couple of emoticons, that are very similar but different, like :-) and :-)). So I think that you will achieve better result if you define your emoticon's array like this:

    $emoticons = array(
        ':-)' => '[HAPPY]',
        ':)' => '[HAPPY]',
        ':o)' => '[HAPPY]',
        ':-(' => '[SAD]',
        ':(' => '[SAD]',
        ...
    )
    

    And when you fill all find/replace definitions, you should reorder this array in a way, that there will be no chance to replace :-)) with :-). I believe if you sort array values by length will be enough. This is in case your are going to use str_replace(). strtr() will do this sort by length automatically!

    If you are concerned about performance, you can check strtr vs str_replace, but I will suggest to make your own testing (you may get different result regarding your $string length and find/replace definitions).

    The easiest way will be if your "find definitions" doesn't contain trailing spaces:

    $string = strtr( $string, $emoticons );
    $emoticons = str_replace( '][', '', trim( join( array_unique( $emoticons ) ), '[]' ) );
    $string = preg_replace( '/\s*\[(' . join( '|', $emoticons ) . ')\]\s*/', '[$1]', $string ); // striping white spaces around word-styled emoticons
    

提交回复
热议问题