繁体   English   中英

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

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

我想从两个字符串( string astring b生成一个唯一键。 string a已经是8至20长度的唯一密钥chars ,而串b不具有8至20相同的长度独特chars 目标密钥必须很难猜测,唯一且具有10个字符的固定长度。 我尝试了mad5()uniqueid()rand()函数的一些组合,但是每次目标字符串超过10个chars的限制。 如何获得10个chars密钥?

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

假设您的目标密钥存储在数据库中。

$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;

希望这会有所帮助。

暂无
暂无

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

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