如何在preg_replace_callback的回调函数中确定主题数组指针的位置?

时间:2013-09-30 09:31:41

标签: arrays pointers position preg-replace-callback

如何在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

1 个答案:

答案 0 :(得分:0)

啊,阿尔玛,谢谢你的帮助。我找到了你可能没有理解我的原因。我寻找的答案太容易了。答案是关键()。对不起,麻烦。谢谢!