javascript 常用函数转换成PHP代码处理
class js2php
{
public static function detect_encoding($text){
$charsets = array(“UTF-8”,“GB2312”,“GBK”,“BIG5”,“ASCII”,“EUC-JP”,‘KOI8-R’,“Shift_JIS”,“CP936”,“ISO-8859-1”,“JIS”,“eucjp-win”,“sjis-win”);
e
n
c
o
d
e
=
(
m
b
d
e
t
e
c
t
e
n
c
o
d
i
n
g
(
encode = (mb_detect_encoding(
encode=(mbdetectencoding(text, $charsets));
return $encode;
}
public static function strlen()
{
static
f
u
n
c
=
n
u
l
l
;
i
f
(
i
s
n
u
l
l
(
func = null; if (is_null(
func=null;if(isnull(func)) {
$func = ‘strlen’;
if (function_exists(‘mb_strlen’)) {
$func = ‘mb_strlen’;
}else if (function_exists(‘iconv_strlen’)) {
$func = ‘iconv_strlen’;
}
}
$args = func_get_args();
e
n
c
o
d
e
=
s
e
l
f
:
:
d
e
t
e
c
t
e
n
c
o
d
i
n
g
(
encode = self::detect_encoding(
encode=self::detectencoding(args[0]);
$args[1] = $encode;
return call_user_func_array($func, $args);
}
public static function substr()
{
static
f
u
n
c
=
n
u
l
l
;
i
f
(
i
s
n
u
l
l
(
func = null; if (is_null(
func=null;if(isnull(func)) {
$func = ‘substr’;
if (function_exists(‘mb_substr’)) {
$func = ‘mb_substr’;
}else if (function_exists(‘iconv_substr’)) {
$func = ‘iconv_substr’;
}
}
$args = func_get_args();
e
n
c
o
d
e
=
s
e
l
f
:
:
d
e
t
e
c
t
e
n
c
o
d
i
n
g
(
encode = self::detect_encoding(
encode=self::detectencoding(args[0]);
$args[3] = $encode;
return call_user_func_array($func, KaTeX parse error: Expected 'EOF', got '}' at position 8: args); }̲ public static …str, KaTeX parse error: Expected '}', got 'EOF' at end of input: …=false) { if (!offset)
o
f
f
s
e
t
=
0
;
r
e
t
u
r
n
s
e
l
f
:
:
o
r
d
u
t
f
8
(
offset = 0; return self::ordutf8(
offset=0;returnself::ordutf8(str, KaTeX parse error: Expected 'EOF', got '}' at position 10: offset); }̲ private static…string, &$offset)
{
c
o
d
e
=
o
r
d
(
s
u
b
s
t
r
(
code = ord(substr(
code=ord(substr(string,
o
f
f
s
e
t
,
1
)
)
;
i
f
(
offset,1)); if (
offset,1));if(code >= 128) { //otherwise 0xxxxxxx
if ($code < 224)
b
y
t
e
s
n
u
m
b
e
r
=
2
;
/
/
110
x
x
x
x
x
e
l
s
e
i
f
(
bytesnumber = 2; //110xxxxx else if (
bytesnumber=2;//110xxxxxelseif(code < 240)
b
y
t
e
s
n
u
m
b
e
r
=
3
;
/
/
1110
x
x
x
x
e
l
s
e
i
f
(
bytesnumber = 3; //1110xxxx else if (
bytesnumber=3;//1110xxxxelseif(code < 248) $bytesnumber = 4; //11110xxx
$codetemp =
c
o
d
e
−
192
−
(
code - 192 - (
code−192−(bytesnumber > 2 ? 32 : 0) - (
b
y
t
e
s
n
u
m
b
e
r
>
3
?
16
:
0
)
;
f
o
r
(
bytesnumber > 3 ? 16 : 0); for (
bytesnumber>3?16:0);for(i = 2; $i <= $bytesnumber; $i++) {
$offset ++;
c
o
d
e
2
=
o
r
d
(
s
u
b
s
t
r
(
code2 = ord(substr(
code2=ord(substr(string, $offset, 1)) - 128; //10xxxxxx
$codetemp = $codetemp*64 + $code2;
}
$code = $codetemp;
}
o
f
f
s
e
t
+
=
1
;
i
f
(
offset += 1; if (
offset+=1;if(offset >= strlen($string)) $offset = -1;
return KaTeX parse error: Expected 'EOF', got '}' at position 7: code; }̲ public static …a, $b)
{
return (
$a >= 0
? $a >>
b
:
(
(
b : ((
b:((a & 0x7fffffff) >>
b
)
∣
(
0
x
40000000
>
>
(
b) | (0x40000000 >> (
b)∣(0x40000000>>(b - 1))
);
}
public static function charAt(
a
,
a,
a,i)
{
return self::substr(
a
,
a,
a,i,1);
}
public static function hex_md5(KaTeX parse error: Expected '}', got 'EOF' at end of input: …r){ return md5(str);
}
public static function toString(
s
t
r
,
str,
str,dec=null){
if(
d
e
c
=
=
16
)
r
e
t
u
r
n
d
e
c
h
e
x
(
dec==16)return dechex(
dec==16)returndechex(str);
return (string)$str;
}
public static function reverse(KaTeX parse error: Expected '}', got 'EOF' at end of input: … array_reverse(arr);
}
public static function string_lenght($string) {
return strlen(iconv(‘UTF-8’, ‘UTF-16LE’, $string)) / 2;
}
public static function charCodeAt($str, $index){
//not working!
c
h
a
r
=
m
b
s
u
b
s
t
r
(
char = mb_substr(
char=mbsubstr(str,
i
n
d
e
x
,
1
,
′
U
T
F
−
8
′
)
;
i
f
(
m
b
c
h
e
c
k
e
n
c
o
d
i
n
g
(
index, 1, 'UTF-8'); if (mb_check_encoding(
index,1,′UTF−8′);if(mbcheckencoding(char, ‘UTF-8’))
{
r
e
t
=
m
b
c
o
n
v
e
r
t
e
n
c
o
d
i
n
g
(
ret = mb_convert_encoding(
ret=mbconvertencoding(char, ‘UTF-32BE’, ‘UTF-8’);
return hexdec(bin2hex($ret));
} else {
return null;
}
}
}
//一段由js代码转换而来的加密算法
<?php class mmb_d{ static $zero = array("0", "00", "000", "0000", "00000", "000000", "0000000", "00000000"); static $chars = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'); static public function strReverse($a) { $b='';$c = array(); for ($b = 0, $l = strlen($a); $b < $l; $b++) $c[] = $a{$b}; return implode('',array_reverse($c)); } static public function encrypt($a, $b, $e) { $a1 = static::shuzi($a); $a2 = static::zimu($a); $a = $a2 . $a1; $f=''; $g = array(); for ($f = 0, $l = strlen($a); $f < $l; $f++) $g[] = static::to(js2php::charCodeAt($a,$f), $b); return static::rnd($e ? static::strReverse(implode("",$g)) : implode("",$g), 4); } static public function to($a, $c) { $e = "" . js2php::toString(static::round($a + 88, $c),16); $f = $c - strlen($e); return $f > 0 ? static::$zero{$f - 1} . $e : $e; } static public function round($a, $b) { $c = 1 << 4 * $b; return 0 > $a ? $a % $c + $c : $a % $c; } static public function shuzi($a) { return preg_replace('/[^0-9]+/i', "",$a); } static public function zimu($a) { return preg_replace('/[^a-zA-Z]+/i', "",preg_replace('/https/', "http",strtolower($a))); } static public function rnd($a, $b) { return static::rd($b) . js2php::hex_md5($a) . static::rd(ceil(('0.'.mt_rand()) * 10)); } static public function rd($a) { $res = ""; for ( $i = 0; $i < $a; $i++) { $res .= static::$chars[ceil(('0.'.mt_rand()) * 35)]; } return $res; } } ?>对应的javascript代码如下: