$arr['CONST_A'] = 'D:\A';
$arr['CONST_A_B'] = 'D:\A\B';
convert('D:\A\B\C', $arr)
should output CONST_A_B . '\C'
How to implement such a function?
$arr['CONST_A'] = 'D:\A';
$arr['CONST_A_B'] = 'D:\A\B';
convert('D:\A\B\C', $arr)
should output CONST_A_B . '\C'
How to implement such a function?
This is a litte cumbersome. It sorts the const array by string length, so it finds the longest possibilities first. Then it compares each string, and on a hit finds the original constant name.
function convert($str, $arr) {
$search = array_combine($arr, array_map("strlen", $arr));
arsort($search);
foreach ($search as $part=>$len) {
if (strncmp($str, $part, $len) == 0) {
$const = array_search($part, $arr);
return "$const . '" . substr($str, $len) . "'";
}
}
}