【发布时间】:2016-08-26 02:44:37
【问题描述】:
我正在尝试写入一个在匿名 preg_replace_callback 函数之外初始化的数组。我已经尝试了“use”关键字,并且还声明了变量“global”,但似乎都不起作用。
这是我的代码:
$headwords=array_keys($dict);
$replaced=array();
// Scan text
foreach($headwords as $index=>$headword){
if(preg_match("/\b".$headword."\b/", $transcript)){
$transcript=preg_replace_callback("/(\b".$headword."\b)/", function($m) use($index, $replaced){
$replaced[$index]=$m[1];
return "<".$index.">";
}, $transcript);
}
}
“$replaced”的 var_dump 将其显示为空数组。 preg_replace_callback 循环是公共类函数的一部分,如果这有什么不同的话。
我试过用谷歌搜索这个问题,但没有成功。 非常感谢您对此问题的任何帮助。
【问题讨论】:
-
你用的是什么php版本?
标签: php regex class scope preg-replace-callback