PHP中通過(guò)Hashids將整數(shù)轉(zhuǎn)化為唯一字符串
需要在前臺(tái)隱藏 ID 的話,可以考慮使用此產(chǎn)品,生成的 id 比較高大上,比較像 Youtube、Youku、Weibo之類的 id 名,比如:XNjkxMTc0MDQ4
官網(wǎng):http://hashids.org/php/Laravel 5 包:https://github.com/vinkla/hashids
PHP中的簡(jiǎn)單實(shí)用示例:
$hashids = new HashidsHashids(’this is my salt’);$id = $hashids->encode(1, 2, 3);$numbers = $hashids->decode($id);
當(dāng)然,包很全,還有 Composer package、Laravel 4 package、Laravel 5 package、CodeIgniter spark、Symfony bundle、Kohana module、Wordpress plugin、CakePHP component、Silex package、Craft plugin featu等等。在官網(wǎng)可以找到鏈接
還支持 JavaScript, Ruby, Python, Java, Scala, PHP, Perl, Swift, Objective-C, C, C++11, Go, Lua, Elixir, ColdFusion, Groovy, Kotlin, Nim, VBA, CoffeeScript and for Node.js & .NET 語(yǔ)言
Laravel 5 使用
使用比較簡(jiǎn)單,而且文檔說(shuō)明得已經(jīng)比較詳細(xì)了。
按文檔說(shuō)明安裝完包后,執(zhí)行
php artisan vendor:publish
操作來(lái)生成配置文件,配置文件路徑:confighashids.php:
’alphabet’=> ’your-alphabet-string’
配置項(xiàng)是你打算讓加密后的字符串里出現(xiàn)什么字符,我用的是 A-Z、a-z、0-9,后來(lái)發(fā)現(xiàn)好像與字符順序有關(guān)系,生成出來(lái)的隨機(jī)字符串大小寫(xiě)與數(shù)字分布不太均勻,就使用 PHP 的 shuffle() 函數(shù)打亂一下順序生成了一組新的 alphabet,之后看起來(lái)就比較高大上了。
使用示例:
// You can alias this in config/app.php.use VinklaHashidsFacadesHashids;Hashids::encode(4815162342);// We’re done here - how easy was that, it just works!Hashids::decode(’doyouthinkthatsairyourebreathingnow’);// This example is simple and there are far more methods available.
相關(guān)文章:
1. CSS可以做的幾個(gè)令你嘆為觀止的實(shí)例分享2. ASP中解決“對(duì)象關(guān)閉時(shí),不允許操作。”的詭異問(wèn)題……3. ASP實(shí)現(xiàn)加法驗(yàn)證碼4. asp讀取xml文件和記數(shù)5. 使用Spry輕松將XML數(shù)據(jù)顯示到HTML頁(yè)的方法6. CSS3中Transition屬性詳解以及示例分享7. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案8. 告別AJAX實(shí)現(xiàn)無(wú)刷新提交表單9. html小技巧之td,div標(biāo)簽里內(nèi)容不換行10. asp批量添加修改刪除操作示例代碼
