PHPize Online / SQLize Online  /  SQLtest Online

A A A
Share      Blog   Popular
Copy Format Clear
Copy Clear
Copy Format Clear
<?php $enc = new sEncoder(); echo $enc->encode(1252365211141),"\n"; echo base_convert(1252365211141, 10, 36),"\n"; echo NewBase60::fromDecimal(1252365211141),"\n"; # strlen('123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'); class sEncoder{ private $allChars = array(); private $codeSize = 0; function __construct() { $this->allChars = array(); $characters = '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; for ($i = 0; $i < strlen($characters); $i++) { for ($j = 0; $j < strlen($characters); $j++) { for ($k = 0; $k < strlen($characters); $k++) { $this->allChars[] = $characters[$i] . $characters[$j] . $characters[$k]; } } } $this->codeSize = sizeof($this->allChars)-1; } public function encode($number){ $number = (int)$number; if($number < $this->codeSize) return $this->allChars[$number]; $a1 = (int)($number / $this->codeSize); $a2 = (int)($number - ($this->codeSize * $a1)); return $this->encode($a1) . $this->allChars[$a2]; } } class NewBase60 { protected static $characterSet = '0123456789ABCDEFGHJKLMNPQRSTUVWXYZ_abcdefghijkmnopqrstuvwxyz'; /** * Convert a sexagesimal number to a decimal number * @param string sexagesimal number to convert * @return integer Decimal representation of sexagesimal number */ public static function toDecimal($sexNum) { // Return falsy and 0 values as is if (!$sexNum) { return $sexNum === '0' ? 0 : $sexNum; } $decNum = 0; foreach(str_split($sexNum) as $chr) { $ord = ord($chr); if ($ord>=48 && $ord<=57) { $ord -= 48; } // 0 - 9 elseif ($ord>=65 && $ord<=72) { $ord -= 55; } // A - H elseif ($ord==73 || $ord==108) { $ord = 1; } // Error correct typo: capital I, lowercase l to 1 elseif ($ord>=74 && $ord<=78) { $ord -= 56; } // J - N elseif ($ord==79) { $ord = 0; } // Error correct typo: capital O to 0 elseif ($ord>=80 && $ord<=90) { $ord -= 57; } // P - Z elseif ($ord==95) { $ord = 34; } // underscore elseif ($ord>=97 && $ord<=107) { $ord -= 62; } // a - k elseif ($ord>=109 && $ord<=122) { $ord -= 63; } // m - z else { $ord = 0; } // treat all other noise as 0 $decNum = 60 *$decNum + $ord; } return $decNum; } /** * Convert a decimal number to a sexagesimal number * @param integer Decimal number to convert * @return string sexagesimal representation of decimal */ public static function fromDecimal($decNum) { $decNum = (int) $decNum; if (!$decNum) { return $decNum === 0 ? '0' : $sexNum; } $aSexCharset = self::$characterSet; $result = ''; while ($decNum > 0) { $decRemainder = $decNum % 60; $decNum = ($decNum - $decRemainder) / 60; $result = $aSexCharset[$decRemainder] . $result; } return $result; } }
Show:  
Copy Clear