我试图在一个数组中调用一个函数,但使用匹配。
我有一个包含文本($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,但是每次函数收到字符串“$2$3”或"\1\2\3“而不是匹配的值时调用函数时。
致以问候!
发布于 2015-07-02 02:40:15
这似乎是不可能的,数组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);
}https://stackoverflow.com/questions/31173792
复制相似问题