57 lines
1.7 KiB
PHP
57 lines
1.7 KiB
PHP
<?php
|
|
|
|
function template_replace_anchor($str, $li) {
|
|
$ptr=0;
|
|
for(;;) {
|
|
$ma=[];
|
|
/*
|
|
replace flag list
|
|
multiple flags can be used together
|
|
|
|
<empty>: raw string. e.g. string
|
|
V: variable-like. e.g. "string"
|
|
N: same as V, but with quotes removed. e.g. string
|
|
U: utf-8 encoded. e.g. \uxxxx\uxxxx\uxxxx
|
|
*/
|
|
preg_match("/#REPLACE-ANCHOR-([A-Z]*?)([0-9]+)#/", $str, $ma, PREG_OFFSET_CAPTURE, $ptr);
|
|
if(count($ma)<=0) {
|
|
break;
|
|
}
|
|
$da="";
|
|
if(array_key_exists((int)$ma[2][0], $li)) {
|
|
$da=$li[$ma[2][0]];
|
|
}
|
|
|
|
$ma[1][0]=strtoupper($ma[1][0]);
|
|
//process flags
|
|
for($a=0; $a<strlen($ma[1][0]); $a++) {
|
|
if($ma[1][0][$a]=="V") {
|
|
@$da=(string)$da;
|
|
$da=json_encode($da, JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE);
|
|
} else if($ma[1][0][$a]=="N") {
|
|
$da=substr($da, 1, -1);
|
|
} else if($ma[1][0][$a]=="U") {
|
|
$da1="";
|
|
for($b=0; $b<mb_strlen($da); $b++) {
|
|
$c=mb_substr($da, $b, 1);
|
|
if(strlen($c)===1) {
|
|
$da1.=$c;
|
|
} else {
|
|
$da1.=sprintf("\\u%04x", mb_ord($c));
|
|
}
|
|
}
|
|
$da=$da1;
|
|
unset($da1);
|
|
unset($b);
|
|
unset($c);
|
|
}
|
|
}
|
|
unset($a);
|
|
//merge string
|
|
$str=substr($str, 0, $ma[0][1]).$da.substr($str, $ma[0][1]+strlen($ma[0][0]));
|
|
$ptr=$ma[0][1]+strlen($da);
|
|
}
|
|
unset($ma);
|
|
unset($da);
|
|
return $str;
|
|
} |