欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

php生成固定長(zhǎng)度純數(shù)字編碼的方法

 更新時(shí)間:2015年07月09日 17:49:31   作者:feiyue  
這篇文章主要介紹了php生成固定長(zhǎng)度純數(shù)字編碼的方法,涉及php字符串與數(shù)組的相關(guān)操作技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下

本文實(shí)例講述了php生成固定長(zhǎng)度純數(shù)字編碼的方法。分享給大家供大家參考。具體如下:

很多時(shí)候我們需要一些固定長(zhǎng)度的數(shù)字編碼,如訂單編號(hào)、卡號(hào)、用戶編號(hào)等等!但是經(jīng)常我們有的是存儲(chǔ)在數(shù)據(jù)庫(kù)中的有序編號(hào),我們可以通過(guò)它直接轉(zhuǎn)成一個(gè)固定長(zhǎng)度的數(shù)字編碼,然后更新到數(shù)據(jù)庫(kù)中形成此記錄的唯一編號(hào)。

<?php
/**
 * 根據(jù)日期或者是給定前綴生成唯一編號(hào)
 * User: minyifei.cn
 * Date: 15/7/7
 */
namespace Minyifei\Libs;
class SequenceNumber {
 /**
  * 根據(jù)顯示寬度獲取指定的 mapbit
  *
  * @param integer $width 編號(hào)顯示寬度
  *
  * @return array
  */
 private static function _getMapbit($width)
 {
  $mapBits = array(
   4=>array(
    10, 2, 11, 3, 0, 1, 9, 7, 12, 6, 4, 8, 5,
   ),
   5=>array(
    4, 3, 13, 15, 7, 8, 6, 2, 1, 10, 5, 12, 0, 11, 14, 9,
   ),
   6=>array(
    2, 7, 10, 9, 16, 3, 6, 8, 0, 4, 1, 12, 11, 13, 18, 5, 15, 17, 14,
   ),
   7=>array(
    18, 0, 2, 22, 8, 3, 1, 14, 17, 12, 4, 19, 11, 9, 13, 5, 6, 15, 10, 16, 20, 7, 21,
   ),
   8=>array(
    11, 8, 4, 0, 16, 14, 22, 7, 3, 5, 13, 18, 24, 25, 23, 10, 1, 12, 6, 21, 17, 2, 15, 9, 19, 20,
   ),
   9=>array(
    24, 23, 27, 3, 9, 16, 25, 13, 28, 12, 0, 4, 10, 18, 11, 2, 17, 1, 21, 26, 5, 15, 7, 20, 22, 14, 19, 6, 8,
   ),
   10=>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, 19, 26, 2,
   ),
   11=>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, 31, 1, 35, 10, 18,
   ),
   12=>array(
    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)];
 }
 /**
  * 格式化給定時(shí)間戳
  *
  * @param integer $ts timestamp, if null use current timestamp
  *
  * @return string
  */
 private static function _fmtTS($ts=null)
 {
  $ts = $ts ?: time();
  return date(self::$_fmt, $ts);
 }
 /**
  * 根據(jù)id獲取一個(gè)隨機(jī)唯一編碼
  * @param $id 編號(hào)
  * @param int $prefix 前綴
  * @param int $width 除前綴外長(zhǎng)度
  * @return string
  */
 public static function generateNumber($id,$prefix=10,$width=8)
 {
  return sprintf("%s%s", $prefix,self::encode($id, $width));
 }
 /**
  * 編碼轉(zhuǎn)換
  *
  * @param integer $id id
  * @param integer $width 編號(hào)額外組成部分的顯示寬度
  *
  * @return integer
  */
 public static function 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);
 }
 /**
  * 獲取唯一編號(hào)
  *
  * @param integer $id id, mostly database primary key
  * @param integer $width 編號(hào)顯示寬度
  * @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));
 }
}

希望本文所述對(duì)大家的php程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • php更新cookie內(nèi)容的詳細(xì)方法

    php更新cookie內(nèi)容的詳細(xì)方法

    在本篇文章里小編給大家整理的是關(guān)于php如何更新cookie內(nèi)容的相關(guān)知識(shí)點(diǎn),有需要的朋友們學(xué)習(xí)下。
    2019-09-09
  • PHP中Array相關(guān)函數(shù)簡(jiǎn)介

    PHP中Array相關(guān)函數(shù)簡(jiǎn)介

    在php教程中數(shù)組是種強(qiáng)大的數(shù)據(jù)類型,他可以做的事情很多,可以存儲(chǔ)不同的數(shù)據(jù)類型在一個(gè)數(shù)組中,下面我們列出了數(shù)組常用的操作,排序,鍵名對(duì)數(shù)組排序等做法。
    2016-07-07
  • 淺析PHP繪圖技術(shù)

    淺析PHP繪圖技術(shù)

    本篇文章是對(duì)PHP繪圖技術(shù)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-07-07
  • PHP實(shí)現(xiàn)數(shù)字補(bǔ)零功能的2個(gè)函數(shù)介紹

    PHP實(shí)現(xiàn)數(shù)字補(bǔ)零功能的2個(gè)函數(shù)介紹

    這篇文章主要介紹了PHP實(shí)現(xiàn)數(shù)字補(bǔ)零功能的2個(gè)函數(shù)介紹,需要的朋友可以參考下
    2014-05-05
  • 防止本地用戶用fsockopen DDOS攻擊對(duì)策

    防止本地用戶用fsockopen DDOS攻擊對(duì)策

    php腳本中的 fsockopen 函數(shù),對(duì)外部地址,通過(guò)UDP發(fā)送大量的數(shù)據(jù)包,攻擊對(duì)方
    2011-11-11
  • 由php中字符offset特征造成的繞過(guò)漏洞詳解

    由php中字符offset特征造成的繞過(guò)漏洞詳解

    這篇文章主要給大家介紹了關(guān)于由php中字符offset特征造成的繞過(guò)漏洞的相關(guān)資料,文中不僅詳細(xì)介紹了該漏洞的形成,更重要的是介紹了修復(fù)方式,對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起看看吧。
    2017-07-07
  • php檢查是否是ajax請(qǐng)求的方法

    php檢查是否是ajax請(qǐng)求的方法

    這篇文章主要介紹了php檢查是否是ajax請(qǐng)求的方法,涉及預(yù)定義服務(wù)器變量的使用技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下
    2015-04-04
  • PHP獲取當(dāng)前頁(yè)面URL函數(shù)實(shí)例

    PHP獲取當(dāng)前頁(yè)面URL函數(shù)實(shí)例

    這篇文章主要介紹了PHP獲取當(dāng)前頁(yè)面URL函數(shù)實(shí)例,講述了一個(gè)非常簡(jiǎn)單實(shí)用的獲取當(dāng)前頁(yè)面URL的函數(shù),并附帶說(shuō)明了server參數(shù)的用法,需要的朋友可以參考下
    2014-10-10
  • PHP fopen函數(shù)用法實(shí)例講解

    PHP fopen函數(shù)用法實(shí)例講解

    我們?cè)诒酒獌?nèi)容中給讀者們整理了關(guān)于PHP fopen函數(shù)的詳細(xì)用法和實(shí)例內(nèi)容,有需要的學(xué)習(xí)下。
    2019-02-02
  • PHP中unset,array_splice刪除數(shù)組中元素的區(qū)別

    PHP中unset,array_splice刪除數(shù)組中元素的區(qū)別

    php中刪除數(shù)組元素是非常的簡(jiǎn)單的,但有時(shí)刪除數(shù)組需要對(duì)索引進(jìn)行一些排序要求我們會(huì)使用到相關(guān)的函數(shù),這里我們來(lái)介紹使用unset,array_splice刪除數(shù)組中的元素區(qū)別吧
    2014-07-07

最新評(píng)論