php如何生成 uuid(总结)
<h1 style="text-align: center">php如何生成 uuid(总结)</h1><h2>一、总结</h2>
<h3>一句话总结:</h3>
<h5>UUID的全拼为“Universally Unique Identifier”,可以译为“通用唯一识别码”。</h5>
<h5>UUID是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的。通常平台 会提供生成UUID的API。UUID按照开放软件基金会(OSF)制定的标准计算,用到了以太网卡地址、纳秒级时间、芯片ID码和许多可能的数字。</h5>
<p> </p>
<h3>1、UUID的格式是什么?</h3>
<h5>UUID格式为:xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx (8-4-4-4-12),其中每个 x 是 0-9 或 a-f 范围内的一个十六进制的数字</h5>
<p> </p>
<h3>2、UUID使用实例?</h3>
<h5>用md5函数生成密码字符串,然后substr函数在里面截取就好:md5(uniqid(mt_rand(), true));</h5>
<div class="cnblogs_code">
<pre><?<span style="color: rgba(0, 0, 0, 1)">php
namespace App\Model\Tools;
</span><span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> Illuminate\Database\Eloquent\Model;
</span><span style="color: rgba(0, 0, 255, 1)">class</span> UUID <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> Model
{
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">function</span> create_uuid(<span style="color: rgba(128, 0, 128, 1)">$prefix</span>=""<span style="color: rgba(0, 0, 0, 1)">){
</span><span style="color: rgba(128, 0, 128, 1)">$chars</span> = <span style="color: rgba(0, 128, 128, 1)">md5</span>(<span style="color: rgba(0, 128, 128, 1)">uniqid</span>(<span style="color: rgba(0, 128, 128, 1)">mt_rand</span>(), <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">));
</span><span style="color: rgba(128, 0, 128, 1)">$uuid</span> = <span style="color: rgba(0, 128, 128, 1)">substr</span> ( <span style="color: rgba(128, 0, 128, 1)">$chars</span>, 0, 8 ) . '-'
. <span style="color: rgba(0, 128, 128, 1)">substr</span> ( <span style="color: rgba(128, 0, 128, 1)">$chars</span>, 8, 4 ) . '-'
. <span style="color: rgba(0, 128, 128, 1)">substr</span> ( <span style="color: rgba(128, 0, 128, 1)">$chars</span>, 12, 4 ) . '-'
. <span style="color: rgba(0, 128, 128, 1)">substr</span> ( <span style="color: rgba(128, 0, 128, 1)">$chars</span>, 16, 4 ) . '-'
. <span style="color: rgba(0, 128, 128, 1)">substr</span> ( <span style="color: rgba(128, 0, 128, 1)">$chars</span>, 20, 12<span style="color: rgba(0, 0, 0, 1)"> );
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">$prefix</span>.<span style="color: rgba(128, 0, 128, 1)">$uuid</span><span style="color: rgba(0, 0, 0, 1)"> ;
}
}</span></pre>
</div>
<p> </p>
<p> </p>
<h3>3、php的UUID扩展?</h3>
<h5>UUID extension</h5>
<p> </p>
<p> </p>
<p> </p>
<h2>二、php生成唯一识别码uuid</h2>
<p>转自或参考:php生成唯一识别码uuid<br>https://www.cnblogs.com/tine/p/9316509.html</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)"><span style="color: rgba(0, 0, 0, 1)">/*生成唯一标志</span><br><span style="color: rgba(0, 0, 0, 1)">*标准的UUID格式为:xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx(8-4-4-4-12)</span><br><span style="color: rgba(0, 0, 0, 1)">*/</span><br><br>function</span><span style="color: rgba(0, 0, 0, 1)">uuid()
{
</span><span style="color: rgba(128, 0, 128, 1)">$chars</span> = <span style="color: rgba(0, 128, 128, 1)">md5</span>(<span style="color: rgba(0, 128, 128, 1)">uniqid</span>(<span style="color: rgba(0, 128, 128, 1)">mt_rand</span>(), <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">));
</span><span style="color: rgba(128, 0, 128, 1)">$uuid</span> = <span style="color: rgba(0, 128, 128, 1)">substr</span> ( <span style="color: rgba(128, 0, 128, 1)">$chars</span>, 0, 8 ) . '-'
. <span style="color: rgba(0, 128, 128, 1)">substr</span> ( <span style="color: rgba(128, 0, 128, 1)">$chars</span>, 8, 4 ) . '-'
. <span style="color: rgba(0, 128, 128, 1)">substr</span> ( <span style="color: rgba(128, 0, 128, 1)">$chars</span>, 12, 4 ) . '-'
. <span style="color: rgba(0, 128, 128, 1)">substr</span> ( <span style="color: rgba(128, 0, 128, 1)">$chars</span>, 16, 4 ) . '-'
. <span style="color: rgba(0, 128, 128, 1)">substr</span> ( <span style="color: rgba(128, 0, 128, 1)">$chars</span>, 20, 12<span style="color: rgba(0, 0, 0, 1)"> );
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">$uuid</span><span style="color: rgba(0, 0, 0, 1)"> ;
}
</span><span style="color: rgba(0, 0, 255, 1)">echo</span>uuid();<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">Returns like 'dba5ce3e-430f-cf1f-8443-9b337cb5f7db'</span></pre>
</div>
<p> </p>
<p> </p>
<h2>三、laravel中uuid使用实例</h2>
<div class="cnblogs_code">
<pre><?<span style="color: rgba(0, 0, 0, 1)">php
namespace App\Model\Tools;
</span><span style="color: rgba(0, 0, 255, 1)">use</span><span style="color: rgba(0, 0, 0, 1)"> Illuminate\Database\Eloquent\Model;
</span><span style="color: rgba(0, 0, 255, 1)">class</span> UUID <span style="color: rgba(0, 0, 255, 1)">extends</span><span style="color: rgba(0, 0, 0, 1)"> Model
{
</span><span style="color: rgba(0, 0, 255, 1)">public</span> <span style="color: rgba(0, 0, 255, 1)">static</span> <span style="color: rgba(0, 0, 255, 1)">function</span> create_uuid(<span style="color: rgba(128, 0, 128, 1)">$prefix</span>=""<span style="color: rgba(0, 0, 0, 1)">){
</span><span style="color: rgba(128, 0, 128, 1)">$chars</span> = <span style="color: rgba(0, 128, 128, 1)">md5</span>(<span style="color: rgba(0, 128, 128, 1)">uniqid</span>(<span style="color: rgba(0, 128, 128, 1)">mt_rand</span>(), <span style="color: rgba(0, 0, 255, 1)">true</span><span style="color: rgba(0, 0, 0, 1)">));
</span><span style="color: rgba(128, 0, 128, 1)">$uuid</span> = <span style="color: rgba(0, 128, 128, 1)">substr</span> ( <span style="color: rgba(128, 0, 128, 1)">$chars</span>, 0, 8 ) . '-'
. <span style="color: rgba(0, 128, 128, 1)">substr</span> ( <span style="color: rgba(128, 0, 128, 1)">$chars</span>, 8, 4 ) . '-'
. <span style="color: rgba(0, 128, 128, 1)">substr</span> ( <span style="color: rgba(128, 0, 128, 1)">$chars</span>, 12, 4 ) . '-'
. <span style="color: rgba(0, 128, 128, 1)">substr</span> ( <span style="color: rgba(128, 0, 128, 1)">$chars</span>, 16, 4 ) . '-'
. <span style="color: rgba(0, 128, 128, 1)">substr</span> ( <span style="color: rgba(128, 0, 128, 1)">$chars</span>, 20, 12<span style="color: rgba(0, 0, 0, 1)"> );
</span><span style="color: rgba(0, 0, 255, 1)">return</span> <span style="color: rgba(128, 0, 128, 1)">$prefix</span>.<span style="color: rgba(128, 0, 128, 1)">$uuid</span><span style="color: rgba(0, 0, 0, 1)"> ;
}
}</span></pre>
</div>
<p> </p>
<p> </p>
<p> </p>
<h2>四、PHP中生成UUID</h2>
<p>转自或参考:PHP中生成UUID<br>https://www.cnblogs.com/cndavidwang/p/4018207.html</p>
<p>一、什么是UUID</p>
<p> </p>
<p>UUID是指在一台机器上生成的数字,它保证对在同一时空中的所有机器都是唯一的。通常平台 会提供生成UUID的API。UUID按照开放软件基金会(OSF)制定的标准计算,用到了以太网卡地址、纳秒级时间、芯片ID码和许多可能的数字。由以 下几部分的组合:当前日期和时间(UUID的第一个部分与时间有关,如果你在生成一个UUID之后,过几秒又生成一个UUID,则第一个部分不同,其余相 同),时钟序列,全局唯一的IEEE机器识别号(如果有网卡,从网卡获得,没有网卡以其他方式获得),UUID的唯一缺陷在于生成的结果串会比较长。关于 UUID这个标准使用最普遍的是微软的GUID(Globals Unique Identifiers)。<br>在ColdFusion中可以用CreateUUID()函数很简单的生成UUID,其格式为:xxxxxxxx-xxxx-xxxx- xxxxxxxxxxxxxxxx(8-4-4-16),其中每个 x 是 0-9 或 a-f 范围内的一个十六进制的数字。而标准的UUID格式为:xxxxxxxx-xxxx-xxxx-xxxxxx-xxxxxxxxxx (8-4-4-4-12)</p>
<p> </p>
<p> </p>
<p> 简单的说UUID就是一串全球唯一的(16进制)数字串。</p>
<p> UUID的全拼为“Universally Unique Identifier”,可以译为“通用唯一识别码”。UUID由开源软件基金会 (Open Software Foundation, OSF) 定义,是分布式计算环境 (Distributed Computing Environment, DCE) 的一个组成部分。</p>
<p> UUID的标准格式为“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxx”,五个部分分别为8个字符、4个字符、4个字符、4个字符、12个字符,中间用“-”号间隔。常见的GUID(Globally Unique Identifier)是微软对UUID标准的一种实现。</p>
<p> </p>
<p>二、为什么要使用UUID</p>
<p> 好处那叫一个多呀~~~,您随便百度把。</p>
<p>三、UUID的生成代码</p>
<p> 可以用扩展。</p>
<p>四、安装UUID扩展</p>
<p> 相关的扩展在这里:PECL :: Package :: uuid。</p>
<p> PHP扩展安装步骤一直就是那几个:</p>
<p> </p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 255, 1)">wget</span> http:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">pecl.php.net/get/uuid-1.0.3.tgz</span>
<span style="color: rgba(0, 0, 255, 1)">tar</span> zxvf uuid-<span style="color: rgba(128, 0, 128, 1)">1.0</span>.<span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">.tgz
cd uuid</span>-<span style="color: rgba(128, 0, 128, 1)">1.0</span>.<span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)">
phpize
.</span>/<span style="color: rgba(0, 0, 0, 1)">configure
</span><span style="color: rgba(0, 0, 255, 1)">make</span>
<span style="color: rgba(0, 0, 255, 1)">make</span> <span style="color: rgba(0, 0, 255, 1)">install</span></pre>
</div>
<p> 好了,然后编辑一下PHP配置文件,重启一下服务器,到phpinfo()去看效果吧:</p>
<p><img src="https://images0.cnblogs.com/blog/565080/201410/110253308901776.png" alt=""></p>
<p> 安装成功之后,写两行代码测试一下吧:</p>
<p> </p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 128, 1)">1</span> <?<span style="color: rgba(0, 0, 0, 1)">php
</span><span style="color: rgba(0, 128, 128, 1)">2</span> <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">uuid.php</span>
<span style="color: rgba(0, 128, 128, 1)">3</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> uuid_create(), "<br />\n"<span style="color: rgba(0, 0, 0, 1)">;
</span><span style="color: rgba(0, 128, 128, 1)">4</span> <span style="color: rgba(0, 0, 255, 1)">echo</span> uuid_create(1); <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">建议用法</span></pre>
</div>
<p> 刷新几次页面,观察一下两行UUID的变化,有什么发现吗?想进一步了解的话,请学习一下UUID的几个版本是如何定义的吧。</p>
<p>五、安装扩展可能遇到的问题</p>
<p> 安装扩展遇到问题一般都是系统缺少相关组件造成的。</p>
<p> 在centos 7中,需要先安装libuuid-devel,这个用yum命令就可以了。</p>
<p> 在mac os 10.9中则需要先安装libuuid,这个要到libuuid | SourceForge.net下载。</p>
<p> </p>
</div>
<div id="MySignature" role="contentinfo">
<div id="fry_added_part">
<div class="fry_website">
<div class="fry_website" style="color:#3c8dbc;padding: 10px 0;display:none;">
我的旨在学过的东西不再忘记(主要使用艾宾浩斯遗忘曲线算法及其它智能学习复习算法)的偏公益性质的完全免费的编程视频学习网站:
【读书编程笔记】fanrenyi.com;有各种前端、后端、算法、大数据、人工智能等课程。
</div>
<div class="copyright" style="color: red;padding-bottom: 10px;">
版权申明:欢迎转载,但请注明出处
<div style="font-size: 12px">一些博文中有一些参考内容因时间久远找不到来源了没有注明,如果侵权请联系我删除。</div>
</div>
<div id="fry_know_friends" style="color: red;padding-bottom: 10px;font-size: 22px;">
<div style="display:none;"></div>
<div style="display:none;">在校每年国奖、每年专业第一,加拿大留学,先后工作于华东师范大学和香港教育大学。</div>
<div style="display:none;">2025-04-30:宅加太忙,特此在网上找女朋友,坐标上海,非诚勿扰,vx:fan404006308</div>
</div>
<div class="copyright" style="color: green;padding-bottom: 10px;">
录播课资料github地址:https://github.com/fry404006308/fry_course_materials
</div>
<div style="display:none;"></div>
</div>
<div class="fry_tech_group" style="color: mediumpurple;">
<div>
AI交流资料群:753014672
</div>
</div>
<div class="fry_recommend">
<h2>作者相关推荐</h2>
<div id="fry_recommend" style="padding-bottom: 40px">
</div>
</div>
<style>
#fry_added_part .inspiration_content{
//max-height: 120px;
overflow: auto;
margin: 20px 0;
}
#fry_added_part .fry_inspiration .simple a{
margin-right: 25px;
}
</style>
<div style="display:none;" class="fry_inspiration">
<div style="color: red;font-size: 20px;text-align: center;" class="title">
感悟总结
</div>
<!--分为详细部分和简略部分-->
<!--详细部分-->
<div class="detail">
<div class="url_set">
<div style="display: none;" class="per_url" href="https://www.cnblogs.com/Renyi-Fan/p/13498246.html"></div>
<!--<div style="display: none;" class="per_url" href="https://www.cnblogs.com/Renyi-Fan/p/14379366.html"></div>-->
<!--<div style="display: none;" class="per_url" href="https://www.cnblogs.com/Renyi-Fan/p/14154624.html"></div>-->
</div>
<div class="content_set"></div>
</div>
<!--简略部分-->
<div style="display: none;" class="simple">
<div style="color: #FF9966;margin-bottom: 10px;">其它重要感悟总结</div>
<div>
感悟总结200813
最近心境200830
最近心境201019
201218-210205
</div>
</div>
</div>
</div><br><br>
来源:https://www.cnblogs.com/Renyi-Fan/p/11870429.html
頁:
[1]