如何在保存匹配规则的数组中调用函数
How to call a function inside an array that save matches rules
我正在尝试在一个数组中调用一个函数,但使用的是匹配项。
我有一个包含文本($text)和 2 个数组的字符串。
数组A有查找内容的规则:
$a=array('rule1', 'rule2', rule3');
数组b有:
$b=array("Rule 1 return the matches: ", "Rule 2: ", "Rule 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 和 $1 进行匹配,但是当我每次函数接收到字符串“$1$2$3”或“”而不是匹配值时调用该函数。
此致!
这个好像不行,保存规则的数组A,是可以用来保存规则的。但是对于 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);
}
我正在尝试在一个数组中调用一个函数,但使用的是匹配项。
我有一个包含文本($text)和 2 个数组的字符串。
数组A有查找内容的规则:
$a=array('rule1', 'rule2', rule3');
数组b有:
$b=array("Rule 1 return the matches: ", "Rule 2: ", "Rule 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 和 $1 进行匹配,但是当我每次函数接收到字符串“$1$2$3”或“”而不是匹配值时调用该函数。
此致!
这个好像不行,保存规则的数组A,是可以用来保存规则的。但是对于 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);
}