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ī)字符串大小寫與數(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. 詳解Android studio 動(dòng)態(tài)fragment的用法2. 解決Android studio xml界面無(wú)法預(yù)覽問(wèn)題3. 基于android studio的layout的xml文件的創(chuàng)建方式4. 圖文詳解vue中proto文件的函數(shù)調(diào)用5. Spring Boot和Thymeleaf整合結(jié)合JPA實(shí)現(xiàn)分頁(yè)效果(實(shí)例代碼)6. 什么是python的自省7. Android如何加載Base64編碼格式圖片8. Vue封裝一個(gè)TodoList的案例與瀏覽器本地緩存的應(yīng)用實(shí)現(xiàn)9. Vuex localStorage的具體使用10. 使用Android studio查看Kotlin的字節(jié)碼教程

網(wǎng)公網(wǎng)安備