简体   繁体   English

从另外两个字符串创建唯一的字符串(一个是唯一的,另一个不是)

[英]create unique string from two other strings (one is unique and other is not)

I want to generate a unique key from two strings , string a and string b . 我想从两个字符串( string astring b生成一个唯一键。 string a is already a unique key of length of 8 to 20 chars , while string b is not unique with same length of 8 to 20 chars . string a已经是8至20长度的唯一密钥chars ,而串b不具有8至20相同的长度独特chars The target key must be hard to guess, unique and has a fixed length of 10 chars. 目标密钥必须很难猜测,唯一且具有10个字符的固定长度。 I have tried some combinations of mad5() , uniqueid() and rand() functions, but target string exceeds the limit of 10 chars everytime. 我尝试了mad5()uniqueid()rand()函数的一些组合,但是每次目标字符串超过10个chars的限制。 How can I get key of 10 chars ? 如何获得10个chars密钥?

您可以将a和b中的所有字符放入一个数组中,对数组进行随机组合,然后将它们随机从0循环到9

Assuming your target key is stored in the db. 假设您的目标密钥存储在数据库中。

$string1 = 'some_unique_string';
$string2 = 'any_other_string';

do{
   $target_string = sub_str(str_shuffle($string1.$string2),0,10);
   $sql='select * from table_name where targetKeyColoumn = "'.$$target_string.'"'; //add sql injection prevention
   $result = mysqli_query($dbConectionResource, $sql);
   $row_cnt = mysqli_num_rows($result);
  }while($row_cnt); //if its 0, come out from loop

echo $target_string;

Hope this will help. 希望这会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM