将emoji表情等多字节字符串转码
<?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;
}
版权属于:Joyber
本文链接:https://blog.qqvbc.com/default/573.html
转载时须注明出处及本声明