欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

这段PHP代码怎么写成能输出的结果和JS一样?

程序员文章站 2022-05-09 17:09:39
...
下面是我自己转成PHP代码
function na($a) {    if (!$a) return "";    $a = (string)$a; $h=array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1);    $i = strlen($a);	$e="";    for ($f = 0;$f> 4);        do{ $aa3=$a[$f++];$c=ord($aa3)&255;if(61==$c)return $e;$c=$h[$c];}while($f> 2);        do{ $aa4=$a[$f++];$b=ord($aa4)&255; if(61==$b)return $e;$b=$h[$b];}while($f  
下面是原JS代码
function na(a) {    if (!a) return "";    var a = a.toString(),        c, b, f, i, e, h = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1];    i = a.length;    f = 0;    for (e = ""; f > 4);        do {            c = a.charCodeAt(f++) & 255;            if (61 == c) return e;            c = h[c]        } while (f > 2);        do {            b = a.charCodeAt(f++) & 255;            if (61 == b) return e;            b = h[b]        } while (f   

我用这个字符串做测试:NAXRSg4bL7zd0vjF8+JxVNXyuUBv1wnLURc=

在PHP测试结果是:4
但在JS里面测试结果是:4ÑJ/¼ÝÒøÅóâqTÕò¹@o× ËQ
不知道哪里错误了


回复讨论(解决方案)

function na($a) {    if (!$a) return "";    $a = "$a";    $h = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1];    $i = strlen($a);    $f = 0;    for ($e = ""; $f > 4);        do {            $c = ord($a{$f++}) & 255;            if (61 == $c) return $e;            $c = $h[$c];        } while ($f > 2);        do {            $b = ord($a{$f++}) & 255;            if (61 == $b) return $e;            $b = $h[$b];        } while ($f   

function na($a) {    if (!$a) return "";    $a = "$a";    $h = [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 62, -1, -1, -1, 63, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -1, -1, -1, -1, -1, -1, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -1, -1, -1, -1, -1];    $i = strlen($a);    $f = 0;    for ($e = ""; $f > 4);        do {            $c = ord($a{$f++}) & 255;            if (61 == $c) return $e;            $c = $h[$c];        } while ($f > 2);        do {            $b = ord($a{$f++}) & 255;            if (61 == $b) return $e;            $b = $h[$b];        } while ($f     
$h数组用[在编辑器上提示有唔。。。。

不过1L的几乎都正确分给你