PHP Mysql-从两个整数生成编码的唯一整数并对其进行解码
我需要从userId生成编码的唯一id,对其进行主题化和解码。编码的数字不应重复,长度必须是确保不会重复的最简单方法,即将两个ID转换为字符串类型,将它们连接起来,然后简单地将它们转换为int或bigint或任何适合您需要的类型。我认为没有必要使用连字符或基转换。例如,userId=12345 subjectId=12345将成为新ID=1234512345,因此如果您以前的ID是唯一的,那么新ID也将是唯一的。谢谢。但是生成的uniqueId不应该在其中显示userId和subject。所以我们对它进行编码。用户应该看不到userId和subjectId。确保不会有重复的最简单方法是将这两个ID转换为字符串类型,将它们连接起来,然后简单地将它们转换为int或bigint或任何适合您需要的类型。我认为没有必要使用连字符或基转换。例如,userId=12345 subjectId=12345将成为新ID=1234512345,因此如果您以前的ID是唯一的,那么新ID也将是唯一的。谢谢。但是生成的uniqueId不应该在其中显示userId和subject。所以我们对它进行编码。用户应该不能看到userId和subjectId。PHP Mysql-从两个整数生成编码的唯一整数并对其进行解码,php,mysql,decode,encode,unique-id,Php,Mysql,Decode,Encode,Unique Id,我需要从userId生成编码的唯一id,对其进行主题化和解码。编码的数字不应重复,长度必须是确保不会重复的最简单方法,即将两个ID转换为字符串类型,将它们连接起来,然后简单地将它们转换为int或bigint或任何适合您需要的类型。我认为没有必要使用连字符或基转换。例如,userId=12345 subjectId=12345将成为新ID=1234512345,因此如果您以前的ID是唯一的,那么新ID也将是唯一的。谢谢。但是生成的uniqueId不应该在其中显示userId和subject。所以我
/* Encode function - Converting from decimal to octal
$decNum = $usreId.'-'.$subjectId;
*/
function convertDecimalToOctal($decNum){
$decToOctalNum ='';
$hyphenPos ='';
$decToOctal ='';
$hyphenPos = !($tmp = strpos($decNum, '-')) ? 0 : $tmp; // Get Hyphen Position from string
$decNum = str_replace('-', '', $decNum); //Replace '-' with '' and get number without '-'
$decToOctal = base_convert($decNum,10,8); // Convert from Decimal to Octal
$decToOctalNum = ($hyphenPos . $decToOctal); // Append the hyphen position to converted octal number
return $decToOctalNum;
}
/* Decode Function - Converting from octal to decimal */
function convertOctalToDecimal($octNum){
$getHyphenPos ='';
$octalToDec ='';
$octalToDecNum ='';
$getHyphenPos = substr($octNum, 0, 1); // get hyphen position(digit) from the octal number
$octNum = substr($octNum, 1); // trim hyphen position off the octal number
$octalToDec = base_convert($octNum,8,10); // Convert from Octal to Decimal
$octalToDecNum = substr($octalToDec, 0, $getHyphenPos) . '-' . substr($octalToDec, $getHyphenPos); // Put the hyphen in the converted decimal number
return $octalToDecNum;
}