我试图在一个数组中调用一个函数,但使用了匹配项。
我有一个包含文本($text(和2个数组的字符串。
数组A具有查找内容的规则:
$a=array('rule1', 'rule2', rule3');
阵列b具有:
$b=array("Rule 1 return the matches: $1 $2 $3", "Rule 2: $1 $2 $3", "Rule 3 $1 $2 $3");
通过foreach循环,数组可以工作:
foreach($a AS $key => $val){
while(preg_match($val, $text)){
$text = preg_replace($val, $b[$key], $text);
}
}
是否存在方法?对于阵列b:
$b=array("Rule 1 return the matches:".calltofunction("$1$2$3")."", ...
我尝试使用''1和$1进行匹配,但每次函数收到字符串"$1$2$3"或"''1''2''3"时,我都会调用该函数,但没有收到匹配的值。
问候!
这似乎不可能,保存规则的数组A,yes可以用于保存规则。但对于B,需要为每个函数制作自定义函数。
function Myfunction($text) {
return preg_replace_callback($a[the num id],
function($match) {
return "This return this matches: $match[1] $match[2] $match[3]";
}
, $text);
}