$subject_array 指针的位置如何在 preg_replace_callback 的回调函数中确定?即什么是数字键?例如
$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