ob_start“冻结”函数内的函数

时间:2014-12-15 21:28:55

标签: php ob-start

我是ob_start的新手,并且我坚持使用此功能!

我必须在文本中解析一些标签,并用函数中的另一个文本返回来替换。 问题是当我调用ob_start时,后面的函数只保留所有序列调用的第一个参数..对不起我的解释,我将代码发布在shell中:

http://codepad.viper-7.com/SjKqS1

<html>
<body>
    <?php

    $data = <<<EOF

last_name,first_name [test name = 12] 卡戴珊,金 汞,[测试名称= 3] dsdss dsddsdsds sabanana dsdsdsds [测试名称=&#34; abcdefg&#34;] frickenoich EOF;

    $tag = 'test';

    $re = "/\\[". $tag ."[\\]| ]/m";

    preg_match_all( $re, $data, $match );

    $re = "/\[" . $tag . " (.*)\=([\"|\'|[:alnum:]]*)]/m";

    $count = preg_match_all( $re, $data, $match, PREG_SET_ORDER ); 

    for( $i = 0; $i < $count; $i++ ) {

        $count_args = count( $match[ $i ] );

        $args = array();

        // if any argouments, make an argouments array
        if( $count_args ) {
            // make array of argouments
            for($arg_idx = 1; $arg_idx < $count_args; $arg_idx += 2){
                $args[ $match[ $i ][ $arg_idx ] ] = $match[ $i ][ $arg_idx + 1 ];
            }
        }
        echo 'name='.$args['name'];
        echo '<br>';

        ob_start();
        call_user_func( 'test', $args );
         $ret_func = ob_get_clean();
        $data = preg_replace( $re, $ret_func, $data);
    }

    function test( $arg ) {

        echo '<br>test(' . $arg['name'] . ')<br>';
    }

    ?>
</body>

1 个答案:

答案 0 :(得分:0)

起初我认为你需要包括ob_end_clean,如:

ob_start();
call_user_func( 'test', $args );
$ret_func = ob_get_clean();
$data = preg_replace( $re, $ret_func, $data);
ob_end_clean();

然后我自己做了一个基本的测试:

<?php
$i = 0;
for($i=0; $i<10; $i++)
{
    ob_start();
    test();
    $ret_func = ob_get_clean();
    echo '<p>' . $ret_func . '</p>';
    ob_end_clean(); //tried with this commented out as well
    echo '<p>' . $ret_func . '</p>';
}
function test()
{
    echo rand(0,100);
}
?>

如果我加入ob_end_clean,它就没有任何区别。所以你的问题实际上可能就是你在循环的每次迭代中重建args数组的方式。

相关问题