【问题标题】:create unique string from two other strings (one is unique and other is not)从其他两个字符串创建唯一字符串(一个是唯一的,另一个不是)
【发布时间】:2017-03-07 19:39:46
【问题描述】:

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

【问题讨论】:

    标签: php string unique


    【解决方案1】:

    你可以将 a 和 b 中的所有字符放入一个数组中,打乱数组,然后在从 0 到 9 的循环中随机拉出它们

    【讨论】:

      【解决方案2】:

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

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

      希望这会有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-07-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多