Home  >  Article  >  Backend Development  >  Convert integers into unique strings through Hashids in php

Convert integers into unique strings through Hashids in php

伊谢尔伦
伊谢尔伦Original
2016-12-02 09:09:402620browse

Convert integers into unique strings through Hashids in php

If you need to hide the ID in the front desk, you can consider using this product. The generated ID is relatively high-end, more like the ID names of Youtube, Youku, Weibo, etc., such as: XNjkxMTc0MDQ4

Official website: http://hashids. org/php/
Laravel 5 package: https://github.com/vinkla/hashids

Simple practical example in PHP:

$hashids = new Hashids\Hashids('this is my salt');
$id = $hashids->encode(1, 2, 3);
$numbers = $hashids->decode($id);

Of course, the package is very complete, including Composer package, Laravel 4 package, Laravel 5 package , CodeIgniter spark, Symfony bundle, Kohana module, WordPress plugin, CakePHP component, Silex package, Craft plugin featu, etc. You can find the link on the official website

Also supports 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 language

Laravel 5 is relatively easy to use

, and the documentation is quite detailed.

After installing the package according to the document instructions, perform the

php artisan vendor:publish

operation to generate the configuration file. The configuration file path: confighashids.php:

'alphabet'=> 'your-alphabet-string'

The configuration item is what characters you want to appear in the encrypted string. I used it They are A-Z, a-z, 0-9. Later I found that it seems to be related to the order of characters. The generated random strings are not evenly distributed in case and number. So I used PHP's shuffle() function to disrupt the order. I bought a new set of alphabet, and it looks taller after that.

Usage example:

// You can alias this in config/app.php.
use Vinkla\Hashids\Facades\Hashids;
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.


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:php injection exampleNext article:php injection example