$arr['CONST_A'] = 'D:\A';
$arr['CONST_A_B'] = 'D:\A\B';
convert('D:\\A\\B\\C', $arr)
should output CONST_A_B . '\\C'
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) . "'";
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.