如何在preg_replace_callback的回调函数中确定$ subject_array指针的位置?即什么是数字键? e.g。
$final_array = preg_replace_callback("/pattern/",
create_function(
'$matches',
'[WHAT IS MY ARRAY POSITION? ($foo = ...)]; return $foo);'
), $subject_array);
修改
我想要Foo,Bar,baZ。以下不起作用:
$rgData = array('foo', 'bar', 'baz');
$rgData = preg_replace_callback('/(\w)(\w)(\w)/',
function($rgMatches) use (&$rgData)
{
var_dump(key($rgData));//see this debug
next($rgData);
if (key($rgData)==2) {
return strtoupper($rgMatches[2]);
} else {
return strtoupper($rgMatches[0]);
}
}, $rgData);
var_dump($rgData);//see this debug
或者更简单,我想要Foo,bAr,baZ:
$rgData = array('foo', 'bar', 'baz');
$rgData = preg_replace_callback('/(\w)(\w)(\w)/',
function($rgMatches) use (&$rgData)
{
var_dump(key($rgData));//see this debug
next($rgData);
return strtoupper($rgMatches[key($rgData)]);
}, $rgData);
var_dump($rgData);//see this debug
答案 0 :(得分:0)