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");
$encode = (mb_detect_encoding($text, $charsets));
return $encode;
}
public static function strlen()
{
static $func = null;
if (is_null($func)) {
$func = 'strlen';
if (function_exists('mb_strlen')) {
$func = 'mb_strlen';
}else if (function_exists('iconv_strlen')) {
$func = 'iconv_strlen';
}
}
$args = func_get_args();
$encode = self::detect_encoding($args[0]);
$args[1] = $encode;
return call_user_func_array($func, $args);
}
public static function substr()
{
static $func = null;
if (is_null($func)) {
$func = 'substr';
if (function_exists('mb_substr')) {
$func = 'mb_substr';
}else if (function_exists('iconv_substr')) {
$func = 'iconv_substr';
}
}
$args = func_get_args();
$encode = self::detect_encoding($args[0]);
$args[3] = $encode;
return call_user_func_array($func, $args);
}
public static function ord($str, $offset=false)
{
if (!$offset) $offset = 0;
return self::ordutf8($str, $offset);
}
private static function ordutf8($string, &$offset)
{
$code = ord(substr($string, $offset,1));
if ($code >= 128) { //otherwise 0xxxxxxx
if ($code < 224) $bytesnumber = 2; //110xxxxx
else if ($code < 240) $bytesnumber = 3; //1110xxxx
else if ($code < 248) $bytesnumber = 4; //11110xxx
$codetemp = $code - 192 - ($bytesnumber > 2 ? 32 : 0) - ($bytesnumber > 3 ? 16 : 0);
for ($i = 2; $i <= $bytesnumber; $i++) {
$offset ++;
$code2 = ord(substr($string, $offset, 1)) - 128; //10xxxxxx
$codetemp = $codetemp*64 + $code2;
}
$code = $codetemp;
}
$offset += 1;
if ($offset >= strlen($string)) $offset = -1;
return $code;
}
public static function unsignedRightShift($a, $b)
{
return (
$a >= 0
? $a >> $b
: (($a & 0x7fffffff) >> $b) | (0x40000000 >> ($b - 1))
);
}
public static function charAt($a,$i)
{
return self::substr($a,$i,1);
}
public static function hex_md5($str){
return md5($str);
}
public static function toString($str,$dec=null){
if($dec==16)return dechex($str);
return (string)$str;
}
public static function reverse($arr){
return 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!
$char = mb_substr($str, $index, 1, 'UTF-8');
if (mb_check_encoding($char, 'UTF-8'))
{
$ret