kotchuprik/php-short-id

又一个短ID生成器。这个库可以帮助你生成像youtube, vimeo, bit.ly等类似的短ID。

1.1 2016-03-25 08:15 UTC

This package is not auto-updated.

Last update: 2024-09-14 17:11:11 UTC


README

该库可以帮助你生成像youtube, vimeo, bit.ly等类似的短ID。短ID生成基于数字ID。

使用场景示例

require('vendor/autoload.php');

$shortId = new \kotchuprik\short_id\ShortId();

为数据库中的记录生成短ID

  1. 当应用程序在你的数据库中创建ID为424242的记录时
  2. $shortId->encode(424242)将其编码为'bLTs'
  3. 你更新了ID为424242的记录,并将其短ID设置为'bLTs'
$id = $shortId->encode(422424);     // $id will be 'bLTs'

// or with $neededLength = 6
$id = $shortId->encode(422424, 6);  // $id will be 'babMwC'

在数据库中搜索记录

  1. 当有人请求rLHWfKd时
  2. $shortId->decode('rLHWfKd')将其解码为424242
  3. 你在数据库中找到了ID为424242的记录
$id = $shortId->decode('bLTs');      // $id will be 424242

// or with $neededLength = 6
$id = $shortId->decode('babMwC', 6); // $id will be 424242