Created
May 19, 2015 14:49
-
-
Save kijtra/e79fbf054f7da54657b0 to your computer and use it in GitHub Desktop.
simple convert integer(system max numeric) <-> string(always 14 chars)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function meconv($value) | |
{ | |
if (ctype_digit(strval($value))) { | |
$str = chr(96 + strlen(base_convert($value, 10, 36))); | |
$str .= base_convert($value, 10, 36); | |
$str .= md5($value); | |
return substr($str, 0, 14); | |
} elseif(is_string($value)) { | |
$len = ord($value{0}) - 96; | |
if ($len >= 1 && $len <= 14) { | |
return base_convert(substr($value, 1, $len), 36, 10); | |
} | |
} | |
return false; | |
} | |
/* | |
meconv(PHP_INT_MAX); // fzik0zjc588c0a | |
meconv('fzik0zjc588c0a'); // 2147483647 | |
meconv('9999999999999998'); // k2qgpckvng1qb7 ('9999999999999998' is system max) | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
DELIMITER // | |
DROP FUNCTION IF EXISTS `MECONV`// | |
CREATE FUNCTION MECONV( _value VARCHAR(20)) RETURNS VARCHAR(20) | |
LANGUAGE SQL NOT DETERMINISTIC READS SQL DATA | |
BEGIN | |
DECLARE re VARCHAR(20) DEFAULT NULL; | |
IF (_value REGEXP '^[0-9]+$') = 1 THEN | |
SET re = SUBSTR(CONCAT(CHAR(96 + CHAR_LENGTH(CONV(_value, 10, 36))), LOWER(CONV(_value, 10, 36)), MD5(_value)), 1, 14); | |
ELSEIF (_value REGEXP '^[a-m][a-z0-9]{13}$') = 1 THEN | |
SET re = CONV(UPPER(SUBSTR(_value, 2, (ASCII(SUBSTR(_value, 1, 1)) - 96))), 36, 10); | |
END IF; | |
RETURN re; | |
END; | |
// | |
/* | |
SELECT meconv(18446744073709551615); // m3w5e11264sgsf (18446744073709551615 is BIGINT max) | |
SELECT meconv('m3w5e11264sgsf'); // 18446744073709551615 | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment