How to write this PHP code can output the same result as JS?

Source: Internet
Author: User
The following is my own transfer to PHP code
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 < $i;) {do {$aa = $a [$f + +]; $c = $h [Ord ($AA) & 255];}        while ($f < $i &&-1 = = $c);        if ( -1 = = $c) break;        Do {$aa 2= $a [$f + +]; $b = $h [Ord ($aa 2) & 255];} while ($f < $i &&-1 = = $b);        if ( -1 = = $b) break; $e + = (string) chr ($c << 2 |        ($b &) >> 4); do{$aa 3= $a [$f + +]; $c =ord ($aa 3) &255;if (61== $c) return $e; $c = $h [$c];}        while ($f < $i &&-1== $c); if ( -1 = = $c) break; $e + = (string) Chr (($b &) << 4 | ($c &Amp        ) >> 2); do{$aa 4= $a [$f + +]; $b =ord ($aa 4) &255; if (61== $b) return $e; $b = $h [$b];}        while ($f < $i &&-1== $b);        if ( -1 = = $b) break;    $e + = (string) Chr (($c &3) << 6 | $b);    }//echo $e; return $e;}

The following is the original JS code
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, 62  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 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    1, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51,-1,-1,-1,-1, 1];    i = a.length;    f = 0; for (E = ""; f < i;)        {Do c = h[a.charcodeat (f++) & 255], while (F < i &&-1 = = c);        if ( -1 = = c) break; Do B = H[a.charcodeat (f++) & 255];        while (f < i &&-1 = = b);        if ( -1 = = b) break; E + = String.fromCharCode (c << 2 |        (b &) >> 4);  do {c = a.charcodeat (f++) & 255;          if (= = = c) return e;        c = H[c]} while (F < i &&-1 = = c);        if ( -1 = = c) break; E + = String.fromCharCode ((b &) << 4 |        (C &) >> 2);            do {b = A.charcodeat (f++) & 255;            if (= = b) return e;        b = H[b]} while (F < i &&-1 = = b);        if ( -1 = = b) break; E + = String.fromCharCode ((c & 3) << 6 | b)} return E}


I use this string to do the test: naxrsg4bl7zd0vjf8+jxvnxyuubv1wnlurc=

The test result in PHP is: 4
But in JS inside the test result is: 4ñj/¼ýòøåóâqtõò¹@oxëq
I don't know where it went wrong.


Reply to discussion (solution)

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, 62, 1, 1, 1, 63, 52, 53, 54, 55, 56, 57, 58, 59 ,-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, 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 < $i;)        {Do $c = $h [Ord ($a {$f + +}) & 255]; while ($f < $i &&-1 = = $c);        if ( -1 = = $c) break; Do $b = $h [Ord ($a {$f + +}) & 255];        while ($f < $i &&-1 = = $b);        if ( -1 = = $b) break; $e. = Chr ($c << 2 |        ($b &) >> 4);            do {$c = Ord ($a {$f + +}) & 255; if (= = = $c) return $E        $c = $h [$c];        } while ($f < $i &&-1 = = $c);        if ( -1 = = $c) break; $e. = Chr (($b &) << 4 |        ($c &) >> 2);            do {$b = Ord ($a {$f + +}) & 255;            if (= = = $b) return $e;        $b = $h [$b];        } while ($f < $i &&-1 = = $b);        if ( -1 = = $b) break;    $e. = Chr (($c & 3) << 6 | $b); } return $e;}

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, 62, 1, 1, 1, 63, 52, 53, 54, 55, 56, 57, 58, 59 ,-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, 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 < $i;)        {Do $c = $h [Ord ($a {$f + +}) & 255]; while ($f < $i &&-1 = = $c);        if ( -1 = = $c) break; Do $b = $h [Ord ($a {$f + +}) & 255];        while ($f < $i &&-1 = = $b);        if ( -1 = = $b) break; $e. = Chr ($c << 2 |        ($b &) >> 4);            do {$c = Ord ($a {$f + +}) & 255; if (= = = $c) return $E        $c = $h [$c];        } while ($f < $i &&-1 = = $c);        if ( -1 = = $c) break; $e. = Chr (($b &) << 4 |        ($c &) >> 2);            do {$b = Ord ($a {$f + +}) & 255;            if (= = = $b) return $e;        $b = $h [$b];        } while ($f < $i &&-1 = = $b);        if ( -1 = = $b) break;    $e. = Chr (($c & 3) << 6 | $b); } return $e;}


$h array with [hints on the editor ....

But the 1L is almost right for you.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.