要在 PHP 中使用 Hashids 并自定义参数设置,请按照以下步骤操作:
首先,确保已经安装了 Composer。然后,在项目根目录运行以下命令来安装 Hashids:composer require hashids/hashids创建一个新的 PHP 文件(例如:hashids_example.php),并在其中包含以下内容:<?phprequire_once 'vendor/autoload.php';use Hashids\Hashids;// 自定义参数设置$salt = 'your-salt-here'; // 自定义盐值,用于增加哈希的唯一性$minHashLength = 10; // 生成的哈希的最小长度$alphabet = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'; // 自定义字母表// 初始化 Hashids 对象$hashids = new Hashids($salt, $minHashLength, $alphabet);// 编码和解码示例$numberToEncode = 12345;$encoded = $hashids->encode($numberToEncode);$decoded = $hashids->decode($encoded);echo "原始数字:{$numberToEncode}\n";echo "编码后的哈希:{$encoded}\n";echo "解码后的数字:" . implode(', ', $decoded) . "\n";更改 $salt 变量以设置自定义盐值。这将影响生成的哈希值,使其具有唯一性。
根据需要调整 $minHashLength 和 $alphabet 变量。$minHashLength 用于设置生成的哈希的最小长度,而 $alphabet 是用于生成哈希的字符集。
保存文件并在命令行中运行该脚本:
php hashids_example.php这将输出原始数字、编码后的哈希以及解码后的数字。通过修改 hashids_example.php 中的自定义参数设置,可以根据需要调整 Hashids 的行为。




