Convert integers to unique strings through Hashids in PHP

Source: Internet
Author: User
Tags new set

Need to hide the ID in the foreground, you can consider using this product, the generated ID is relatively tall, compared to such as Youtube, Youku, Weibo, such as the ID name, such as: Xnjkxmtc0mdq4

Official website: http://hashids.org/php/
Laravel 5 pack: Https://github.com/vinkla/hashids

Simple and practical examples 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 complete, and there are Composer packages, Laravel 4, Laravel 5 package, CodeIgniter Spark, Symfony Bundle, Kohana module, Wordpress Plugin, CakePHP component, Silex package, Craft plugin Featu and more. Links can be found on the official website

also supports JavaScript, Ruby, Python, Java, Scala, PHP, Perl, Swift, Objective-c, C, c++11, Go, Lua, Elixir, ColdFusion, Groovy, K Otlin, Nim, VBA, Coffeescript and for node. JS &. NET language

Laravel 5 Use

The use is relatively simple, and the documentation has been more detailed.

After the package is installed, follow the instructions in the documentation

PHP Artisan Vendor:publish

Action to generate the configuration file, Profile path: config\hashids.php:

' Alphabet ' = ' your-alphabet-string '

Configuration item is what character you intend to let in the encrypted string, I use a-Z, A-Z, 0-9, and later found that it seems to be related to the character sequence, generated random string case and the number distribution is not very uniform, the use of PHP shuffle () function scrambled order generated a new set of Alphabet, and then it looks bigger.

Examples of Use:

You can alias this in Config/app.php.use Vinkla\hashids\facades\hashids; Hashids::encode (4815162342);//We do here-how Easy is that, it just works! Hashids::d ecode (' Doyouthinkthatsairyourebreathingnow ');//This example was simple and there be far more methods available .
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.