PHP generates a fixed-length, pure-coded method, and PHP generates length-coded
In this paper, we describe the method of generating a fixed length pure digital encoding in PHP. Share to everyone for your reference. Specific as follows:
Many times we need some fixed-length digital coding, such as order number, card number, user number, etc. But often we have an ordered number stored in a database that we can directly convert to a fixed-length numeric encoding, and then update to the database to form a unique number for this record.
<?php/** * Generates a unique number based on a date or a given prefix * User:minyifei.cn * DATE:15/7/7 */namespace minyifei\libs;class sequencenumber {/** * Gets the specified mapbit * * @param integer $width number display width according to the display width * * @return array */private static function _getmapbit ($width) {$mapBits = Array (4=>array (2, 3, 0, 1, 9, 7,, 6, 4, 8, 5,), 5=>array (4, 3, 13, 15, 7, 8 , 6, 2, 1, ten, 5, 0, 9, one,,,), 6=>array (2, 7, 10, 9, 16, 3, 6, 8, 0, 4, 1, 12, 11, 13, 18, 5, 15, 17, 1 4,), 7=>array (0, 2, 8, 3, 1, +,, 4, 9,, 5,, 6,, 7,,), 8=>a Rray (one, 8, 4, 0, 7, 3, 5,,,, 1, 6,, 2, 9,,,,), and 9=>array ( 3, 9, 0, 2, 4, ten, 1, one, 5, 7, +, 6,,, (+), +, 8, +--------- T;array (32, 3, 1, 28, 21, 18, 30, 7, 12, 22, 20, 13, 16, 15, 6, 17, 9, 25, 11, 8, 4, 27, 14, 31, 5, 23, 24, 29, 0, 10, 11=&G, 2,)T;array (9, 13, 2, 29, 11, 32, 14, 33, 24, 8, 27, 4, 22, 20, 5, 0, 21, 25, 17, 28, 34, 6, 23, 26, 30, 3, 7, 19, 16, 15, 12=>array, 1, 31, 4, 16, 33, 35, 29, 17, 37, 12, 28, 32, 22, 7, 10, 14, 26, 0, 9, 8, 3, 20, 2, 13, 5, 36, 27, 23, 15, 19, 34, 38, 11, 24, 25, 30, 21, 18, 6, 1,),); return $mapBits [Intval ($width)]; }/** * Format the given timestamp * * @param integer $ts timestamp, if NULL use current timestamp * * @return String */Private Stati C function _fmtts ($ts =null) {$ts = $ts?: Time (); Return Date (self::$_fmt, $ts); /** * Gets a randomly unique encoding based on the ID * @param $id number * @param int $prefix prefix * @param int $width except prefix Foreign minister * @return String * * public static function Generatenumber ($id, $prefix =10, $width =8) {return sprintf ("%s%s", $prefix, Self::encode ($id, $width));} /** * Code Conversion * * @param integer $id ID * @param integer $width number additional component display width * * @return integer */public static fun Ction encode ($id, $width) {$maximum = Intval (str_repeat (9, $width)); $superscript = intval (log ($maximum)/log (2)); $r = 0; $sign = 0x1 << $superscript; $id |= $sign; $mapbit = Self::_getmapbit ($width); for ($x = 0; $x < $superscript; $x + +) {$v = ($id >> $x) & 0x1; $r |= ($v << $mapbit [$x]); } $r + = $maximum-pow (2, $superscript) + 1; Return sprintf ("%0${width}s", $r); }/** * Get unique number * * @param integer $id ID, mostly database primary key * @param integer $width number display width * @param integer $ts Timestamp * * @return String */public static function Get ($id, $width, $ts =null) {return sprintf ('%s%s ', Self:: _fmtts ($ts), Self::encode ($id, $width)); }}
I hope this article is helpful to everyone's PHP programming.
http://www.bkjia.com/PHPjc/1029584.html www.bkjia.com true http://www.bkjia.com/PHPjc/1029584.html techarticle PHP generates a fixed-length pure digital encoding method, PHP generation length Coding This article describes the PHP generation of fixed-length pure digital encoding method. Share to everyone for your reference. Specific as follows ...