<?php

    /**
     * 将字符串中编码的emoji字符还原
     * @param $text
     * @return string
     */
    function decodeEmoji($text)
    {
        $content = $text;
        if (preg_match_all("/(\[\:[^\]]+\])/", $content, $match)) {
            foreach ($match[1] as $k=>$str) {
                $str2 = '"' . str_replace(['[', ']', ':'], ['', '', '\\'], $str) . '"';
                $content = str_replace($str, json_decode($str2), $content);
            }
            return $content;
        }
        return $text;
    }

    /**
     * 将字符串中的emoji字符编码
     * @param $text
     * @return string
     */
    function encodeEmoji($text)
    {
        $content = '';
        foreach (preg_split("//u", $text, null, PREG_SPLIT_NO_EMPTY) as $v) {
            $content .= strlen($v)==4 ? '['.str_replace(['\\','"'], [':',''], json_encode($v)).']' : $v;
        }
        return $content;
    }

标签: none

添加新评论