How to generate unique strings in php?

WBOY
Release: 2016-09-25 09:37:03
Original
1070 people have browsed it

How to use php to generate unique strings?
Similar to the string that will appear at the end of youtube: 6Af6b_wyiwI (acceptable characters in the URL)
And is it case-sensitive?
And you can set the length of the string that appears each time
For example, 4~7 random
will become ga79e aa09gsw awe9f and so on (it will change like this every time

Reply content:

How to use php to generate unique strings?
Just like the string that will appear at the end of youtube: 6Af6b_wyiwI (acceptable characters in the URL)
And is it case-sensitive?
And you can set the length of the string that appears each time
For example, 4~7 random
will become ga79e aa09gsw awe9f and so on (it will change like this every time

It is recommended to use timestamp, which can be encrypted by MD5 or SHA1, but the length is fixed and case-fixed. It can be intercepted, or you can define the encryption rules yourself. If you define it yourself, you can set the desired length. There is no possibility of duplication in this way (an impossible event)
If you define the string yourself and then obtain it randomly, there is a possibility of duplication (a small probability event).
If you just write the URL
You can also refer to segmentfault’s URL writing method

https://segmentfault.com/q/10...

Generate a sequentially growing sequence, and then convert it to a string through ascii code

PHP decimal and hexadecimal conversion:

<code>echo gmp_strval(gmp_init('9876543210', 10), 62)."\n"; //AmOy42
echo gmp_strval(gmp_init('AmOy42', 62), 10)."\n";     //9876543210</code>
Copy after login

The AmOy42 above should be the "string" you want. In fact, it is a 62-base number.
62-base (10+26+26) uses 10 numbers + 26 lowercase letters + 26 uppercase letters. Represents a numerical value, thereby greatly reducing the number of digits.
9876543210 is the self-increasing ID in the database, thus achieving uniqueness.

Let’s talk about a simple method, use hash algorithms such as sha256 to obtain binary data (instead of hexadecimal strings), and then use base64 to encode it

This reference short link generation method is OK

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!