如何在PHP中制作短随机唯一密钥(例如YouTube视频ID)? [英] How can I make short random unique keys, like YouTube video IDs, in PHP?

查看:138
本文介绍了如何在PHP中制作短随机唯一密钥(例如YouTube视频ID)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么方法可以创建唯一的键,例如YouTube视频网址中使用的键(例如: https://www.youtube .com/watch?v = nWChTnkVdKE )?

Is there any way to create unique keys like those used in YouTube video urls (ex: https://www.youtube.com/watch?v=nWChTnkVdKE)?

推荐答案

该想法是在其他数学基础中转换唯一的整数(例如当前时间).

The idea is to convert a unique integer (such as current time) in an other mathematical base.

PHP中非常简单的方法:

A very simple way in PHP :

// With this precision (microsecond) ID will looks like '2di2adajgq6h'

$id = base_convert(microtime(false), 10, 36);


// With less precision (second) ID will looks like 'niu7pj'

$id = base_convert(time(), 10, 36);

这篇关于如何在PHP中制作短随机唯一密钥(例如YouTube视频ID)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆