Search code examples
phpstringunique

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 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. The target key must be hard to guess, unique and has a fixed length of 10 chars. I have tried some combinations of mad5(),uniqueid() and rand() functions, but target string exceeds the limit of 10 chars everytime. How can I get key of 10 chars?


Solution

  • 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.