Home > Backend Development > PHP Tutorial > 这段PHP代码如何写成能输出的结果和JS一样

这段PHP代码如何写成能输出的结果和JS一样

WBOY
Release: 2016-06-13 12:08:50
Original
887 people have browsed it

这段PHP代码怎么写成能输出的结果和JS一样?
下面是我自己转成PHP代码

<br />function na($a) {<br />    if (!$a) return "";<br />    $a = (string)$a;<br /> $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);<br />    $i = strlen($a);<br />	$e="";<br />    for ($f = 0;$f<$i;){<br />        do {$aa=$a[$f++];$c = $h[ord($aa) & 255];}while ($f < $i && -1 == $c);<br />        if (-1 == $c) break;<br />        do {$aa2=$a[$f++];$b = $h[ord($aa2) & 255];} while ($f < $i && -1 == $b);<br />        if (-1 == $b) break;<br />        $e += (string)chr($c << 2 | ($b & 48) >> 4);<br />        do{ $aa3=$a[$f++];$c=ord($aa3)&255;if(61==$c)return $e;$c=$h[$c];}while($f<$i&&-1==$c);<br />        if (-1 == $c) break;<br />		$e += (string)chr(($b & 15) << 4 | ($c & 60) >> 2);<br />        do{ $aa4=$a[$f++];$b=ord($aa4)&255; if(61==$b)return $e;$b=$h[$b];}while($f<$i&&-1==$b);<br />        if (-1 == $b) break;<br />        $e += (string)chr(($c &3) << 6 | $b);<br />    }<br />	//echo $e;<br />    return $e;<br />}<br />
Copy after login

下面是原JS代码
<br />function na(a) {<br />    if (!a) return "";<br />    var a = a.toString(),<br />        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];<br />    i = a.length;<br />    f = 0;<br />    for (e = ""; f < i;) {<br />        do c = h[a.charCodeAt(f++) & 255]; while (f < i && -1 == c);<br />        if (-1 == c) break;<br />        do b = h[a.charCodeAt(f++) & 255]; while (f < i && -1 == b);<br />        if (-1 == b) break;<br />        e += String.fromCharCode(c << 2 | (b & 48) >> 4);<br />        do {<br />            c = a.charCodeAt(f++) & 255;<br />            if (61 == c) return e;<br />            c = h[c]<br />        } while (f < i && -1 == c);<br />        if (-1 == c) break;<br />        e += String.fromCharCode((b & 15) << 4 | (c & 60) >> 2);<br />        do {<br />            b = a.charCodeAt(f++) & 255;<br />            if (61 == b) return e;<br />            b = h[b]<br />        } while (f < i && -1 == b);<br />        if (-1 == b) break;<br />        e += String.fromCharCode((c & 3) << 6 | b)<br />    }<br />    return e<br />}<br />
Copy after login


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

在PHP测试结果是:4
但在JS里面测试结果是:4?Jmail protected] ?Q
不知道哪里错误了
------解决思路----------------------
function na($a) {<br />    if (!$a) return "";<br />    $a = "$a";<br />    $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];<br />    $i = strlen($a);<br />    $f = 0;<br />    for ($e = ""; $f < $i;) {<br />        do $c = $h[ord($a{$f++}) & 255]; while ($f < $i && -1 == $c);<br />        if (-1 == $c) break;<br />        do $b = $h[ord($a{$f++}) & 255]; while ($f < $i && -1 == $b);<br />        if (-1 == $b) break;<br />        $e .= chr($c << 2 <br><font color='#FF8000'>------解决思路----------------------</font><br> ($b & 48) >> 4);<br />        do {<br />            $c = ord($a{$f++}) & 255;<br />            if (61 == $c) return $e;<br />            $c = $h[$c];<br />        } while ($f < $i && -1 == $c);<br />        if (-1 == $c) break;<br />        $e .= chr(($b & 15) << 4 <br><font color='#FF8000'>------解决思路----------------------</font><br> ($c & 60) >> 2);<br />        do {<br />            $b = ord($a{$f++}) & 255;<br />            if (61 == $b) return $e;<br />            $b = $h[$b];<br />        } while ($f < $i && -1 == $b);<br />        if (-1 == $b) break;<br />        $e .= chr(($c & 3) << 6 <br><font color='#FF8000'>------解决思路----------------------</font><br> $b);<br />    }<br />    return $e;<br />}<br />
Copy after login

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template