首頁 後端開發 php教程 PHP把数目字ID转字母ID

PHP把数目字ID转字母ID

Jun 13, 2016 am 11:34 AM
index out quot substr

PHP把数字ID转字母ID

PHP把数字ID转字母ID

 ID是网站中经常出现的,它一般是数字,但是我们发现现在的网站很多ID都是字母了,比如YouTube的视频播放页它的URL类似/watch?v=yzNjIBEdyww。 下面是一个生成字母ID的方法。
使用示例:

echo alphaID(12354);  //qnd  echo alphaID('qnd',true); //12354  echo alphaID(12354,false,6); //qndaab  echo alphaID('qndaab',true, 6); //12354 
登入後複製

源码:

<?php /** * Translates a number to a short alhanumeric version * * Translated any number up to 9007199254740992 * to a shorter version in letters e.g.: * 9007199254740989 --> PpQXn7COf * * specifiying the second argument true, it will * translate back e.g.: * PpQXn7COf --> 9007199254740989 * * this function is based on any2dec && dec2any by * fragmer[at]mail[dot]ru * see: http://nl3.php.net/manual/en/function.base-convert.php#52450 * * If you want the alphaID to be at least 3 letter long, use the * $pad_up = 3 argument * * In most cases this is better than totally random ID generators * because this can easily avoid duplicate ID's. * For example if you correlate the alpha ID to an auto incrementing ID * in your database, you're done. * * The reverse is done because it makes it slightly more cryptic, * but it also makes it easier to spread lots of IDs in different * directories on your filesystem. Example: * $part1 = substr($alpha_id,0,1); * $part2 = substr($alpha_id,1,1); * $part3 = substr($alpha_id,2,strlen($alpha_id)); * $destindir = "/".$part1."/".$part2."/".$part3; * // by reversing, directories are more evenly spread out. The * // first 26 directories already occupy 26 main levels * * more info on limitation: * - http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/165372 * * if you really need this for bigger numbers you probably have to look * at things like: http://theserverpages.com/php/manual/en/ref.bc.php * or: http://theserverpages.com/php/manual/en/ref.gmp.php * but I haven't really dugg into this. If you have more info on those * matters feel free to leave a comment. * * @author  Kevin van Zonneveld  * @author  Simon Franz * @author  Deadfish * @copyright 2008 Kevin van Zonneveld (http://kevin.vanzonneveld.net) * @license   http://www.opensource.org/licenses/bsd-license.php New BSD Licence * @version   SVN: Release: $Id: alphaID.inc.php 344 2009-06-10 17:43:59Z kevin $ * @link    http://kevin.vanzonneveld.net/ * * @param mixed   $in    String or long input to translate * @param boolean $to_num  Reverses translation when true * @param mixed   $pad_up  Number or boolean padds the result up to a specified length * @param string  $passKey Supplying a password makes it harder to calculate the original ID * * @return mixed string or long */function alphaID($in, $to_num = false, $pad_up = false, $passKey = null){  $index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";  if ($passKey !== null) {    // Although this function's purpose is to just make the    // ID short - and not so much secure,    // with this patch by Simon Franz (http://blog.snaky.org/)    // you can optionally supply a password to make it harder    // to calculate the corresponding numeric ID     for ($n = 0; $n<strlen substr hash strlen : for array_multisort sort_desc implode if digital number alphabet letter code strrev bcpow strpos> 0) {        $out -= pow($base, $pad_up);      }    }    $out = sprintf('%F', $out);    $out = substr($out, 0, strpos($out, '.'));  } else {    // Digital number  -->>  alphabet letter code    if (is_numeric($pad_up)) {      $pad_up--;      if ($pad_up > 0) {        $in += pow($base, $pad_up);      }    }     $out = "";    for ($t = floor(log($in, $base)); $t >= 0; $t--) {      $bcp = bcpow($base, $t);      $a   = floor($in / $bcp) % $base;      $out = $out . substr($index, $a, 1);      $in  = $in - ($a * $bcp);    }    $out = strrev($out); // reverse  }   return $out;}</strlen>
登入後複製


本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
3 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1664
14
CakePHP 教程
1423
52
Laravel 教程
1318
25
PHP教程
1269
29
C# 教程
1248
24
index.html是什麼文件? index.html是什麼文件? Feb 19, 2024 pm 01:36 PM

index.html代表網頁的首頁文件,是網站的預設頁面。當使用者造訪一個網站時,通常會先載入index.html頁面。 HTML(HypertextMarkupLanguage)是一種用來建立網頁的標記語言,index.html也是一種HTML檔。它包含網頁的結構和內容,以及用於格式化和佈局的標籤和元素。下面是一個範例的index.html程式碼:&lt

主機板上的數位音訊輸出介面-SPDIF OUT 主機板上的數位音訊輸出介面-SPDIF OUT Jan 14, 2024 pm 04:42 PM

主機板上SPDIFOUT連接線序最近我遇到了一個問題,就是關於電線的接線順序。我上網查了一下,有些資料說1、2、4對應的是out、+5V、接地;而有些資料則說1、2、4對應的是out、接地、+5V。最好的方法是查看你的主機板說明書,如果找不到說明書,你可以使用萬用電表進行測量。首先找到接地,然後就可以確定其他的接線順序了。主機板vdg怎麼接線連接主機板的VDG接線時,您需要將VGA連接線的一端插入顯示器的VGA接口,另一端插入電腦的顯示卡VGA接口。請注意,不要將其插入主機板的VGA介面。完成連接後,您可以

out和in介面是什麼意思 out和in介面是什麼意思 Sep 28, 2021 pm 04:39 PM

out介面指的是輸出接口,in接口指的是輸入接口。 out介面一般代表著音源線路輸出接口,用來接負載,例音箱、耳機等;而in接口一般代表著音源線路輸入接口,用來接CD機、手機、MP3、電腦等。

PHP傳回字串第一個字元的 ASCII 值 PHP傳回字串第一個字元的 ASCII 值 Mar 21, 2024 am 11:01 AM

這篇文章將為大家詳細講解有關PHP返回字符串第一個字符的ASCII值,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章後可以有所收穫。 PHP傳回字串第一個字元的ASCII值引言在php中,取得字串第一個字元的ASCII值是一個常見的操作,涉及到字串處理和字元編碼基礎知識。 ASCII值用於表示字元在電腦系統中的數字值,對於字元比較、資料傳輸和儲存至關重要。過程取得字串第一個字元的ASCII值涉及以下步驟:取得字串:確定要取得ASCII值的字串。它可以是變數、字串常數

PHP傳回一個字串在另一個字串中開始位置到結束位置的字串 PHP傳回一個字串在另一個字串中開始位置到結束位置的字串 Mar 21, 2024 am 10:31 AM

這篇文章將為大家詳細講解有關PHP返回一個字符串在另一個字符串中開始位置到結束位置的字符串,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章後可以有所收穫。 PHP中使用substr()函數從字串中擷取子字串substr()函數可從字串中擷取指定範圍內的字元。其語法如下:substr(string,start,length)其中:string:要從中提取子字串的原始字串。 start:子字串開始位置的索引(從0開始)。 length(可選):子字串的長度。如果未指定,則提

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

PHP原始碼運行問題:index報錯解決方法 PHP原始碼運行問題:index報錯解決方法 Mar 09, 2024 pm 09:24 PM

PHP原始碼運行問題:index報錯解決方法,需要具體程式碼範例PHP是一種廣泛使用的伺服器端腳本語言,經常被用於開發動態網站和Web應用程式。然而,有時候在運行PHP原始碼時會遇到各種問題,其中「index報錯」是比較常見的情況。本文將介紹一些常見的index報錯原因以及解決方法,並提供具體的程式碼範例,幫助讀者更好地處理這類問題。問題描述:在執行PHP程式時

C語言中go out的用法詳解 C語言中go out的用法詳解 Mar 14, 2024 am 08:27 AM

在C語言中,"goout"是一個常用的術語,指的是函數的退出和傳回值的傳遞。在本文中,我們將詳細解釋C語言中"goout"的用法,並提供具體的程式碼範例。在C語言中,函數的回傳值透過return語句傳遞給呼叫函數。 return語句用來終止函數的執行,並傳回一個值給呼叫者。在函數體中的任何地方都可以使用return語句,但一般我們會在函數的末尾使用它來傳回最

See all articles