php算法代码,emoji表情换成自己喜欢的就行
- post请求
- api源码
<?php
// 可以封装成以下的API代码,接受GET请求,参数包括:
// action:指定API动作,encode表示加密,decode表示解密;
// content:需要加密或解密的字符串,使用urlencode编码;
$miyu = ['表情1','表情2','表情3','表情4','表情5','表情6','表情7','表情8','表情9','表情10'];
function encode($str, $miyu)
{
$code = null;
$hexArray = str_split_unicode(bin2hex($str));
foreach ($hexArray as $k => $v) {
$x = base_convert($v, 16, 10) + $k % 16;
if ($x >= 16) {
$x -= 16;
}
$code .= $miyu[($x / 10)] . $miyu[$x % 10];
}
return $code;
}
function decode($str, $miyu)
{
$code = null;
$hexArray = str_split_unicode($str);
$n = count($hexArray);
for ($i = 0; $i < $n; $i++) {
if ($i % 2 == 0) {
if (empty($hexArray[$i + 1])) {
break;
}
$A = array_search($hexArray[$i], $miyu);
$B = array_search($hexArray[$i + 1], $miyu);
$x = (($A * 10) + $B) - (($i / 2) % 16);
if ($x < 0) {
$x += 16;
}
$code .= dechex($x);
}
}
return pack("H*", $code);
}
function str_split_unicode($str, $l = 0)
{
if ($l > 0) {
$ret = array();
$len = mb_strlen($str, "UTF-8");
for ($i = 0; $i < $len; $i += $l) {
$ret[] = mb_substr($str, $i, $l, "UTF-8");
}
return $ret;
}
return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);
}
function daddslashes($string, $force = 0, $strip = FALSE)
{
!defined('MAGIC_QUOTES_GPC') && define('MAGIC_QUOTES_GPC', get_magic_quotes_gpc());
if (!MAGIC_QUOTES_GPC || $force) {
if (is_array($string)) {
foreach ($string as $key => $val) {
$string[$key] = daddslashes($val, $force, $strip);
}
} else {
$string = addslashes($strip ? stripslashes($string) : $string);
}
}
return $string;
}
if (isset($_POST['action']) && isset($_POST['content'])) {
$action = daddslashes($_POST['action']);
$content = daddslashes($_POST['content']);
if ($action == 'encode') {
$result = encode($content, $miyu);
echo json_encode(array('error' => 200, 'data' => $result, 'message' => '加密成功'), JSON_UNESCAPED_UNICODE);
} else if ($action == 'decode') {
$result = decode($content, $miyu);
echo json_encode(array('error' => 200, 'data' => $result, 'message' => '解密成功'), JSON_UNESCAPED_UNICODE);
} else {
echo 'Invalid action';
}
} else {
echo 'Invalid parameters';
}
?>
版权声明:《 好玩的聊天内容转emoji表情加解密算法 》为4204235原创文章,转载请注明出处!
最后编辑:2024-7-10 20:07:07

提示:本文章评论功能已关闭