I have a 16-character, input string composed of hexadecimal characters. I need to isolate the last two characters, then replace the first character using an associative lookup/map, then extend the string from two to four characters by repeating the characters.
My sample input is:
$atcode = '11F5796B61690196';
My associative array is:
$sub1 = [
'0' => '2',
'1' => '3',
'2' => '0',
'3' => '1',
'4' => '6',
'5' => '7',
'7' => '5',
'8' => 'A',
'9' => 'B',
'A' => '8',
'B' => '9',
'C' => 'E',
'D' => 'F',
'E' => 'C',
'F' => 'D'
];
The isolated characterd should be 9
and 6
.
The desired result is: B6B6
This is the code so far:
$codearray = str_split($atcode);
//select the 15th digit from $codearray and name it $one
$one = $codearray[14];
//create an associative array and name it $sub1
$sub1 = array('0' => '2', '1' => '3', '3' => '1', 'D' => 'F', '4' => '6', '5' => '7', '7' => '5', '8' => 'A', '9' => 'B', 'A' => '8', 'B' => '9', 'C' => 'E', '2' => '0', 'E' => 'C', 'F' => 'D');
//search through $sub1, determine which value is equivalent to $one, and select its equivalency from the key to value pairs
$codeone = array_replace($one, $sub1);
//select the 16th digit from $codearray and name it $two
$two = $codearray[15];
echo "$codeone", "$two";
However, I am just getting a blank page.
Abandon most of that code.
Extract the last two characters, translate the first, then repeat them.
Code: (Demo)
var_export(
str_repeat(
strtr(
$atcode[14],
'012345789ABCDEF',
'2301675AB89EFCD'
) . $atcode[15],
2
)
);
Or if you want to use the associative array: Demo
var_export(
str_repeat(
strtr($atcode[14], $sub1) . $atcode[15],
2
)
);
Here's an alternative, for the regex lovers: Demo
var_export(
preg_replace_callback(
'/.*(.)(.)/',
fn($m) => str_repeat(strtr($m[1], $sub1) . $m[2], 2),
$atcode
)
);