preg_replace($skx,$imsz2,$neirong);如:$neirong中有多个$skx 我需要每次替换都能得到一个不同的ID 就像if一样 有没办法办到

解决方案 »

  1.   


    如$neirong中有包含3个$skx  把第一个替换成1  第二个替换成2  第三个替换成3  就像if加循环ID一样
      

  2.   

    当你需要一个solution的时候,你需要做的是描述你的问题,详细的描述你的问题,其他的都没有意义。根据你的说法,你想要的是sprintf函数,而不是str_replace
      

  3.   

    preg_replace_callback:
    <?php$str='this is a test for this string includes many this';$replace='/this/x';$result=preg_replace_callback(
            $replace,
            function($ms){
             static $i;
             $i=$i+1;
                return "that($i)";
            },
            $str
        );echo $result,"\n";
      

  4.   

    [code]
    <?php
    $str='this is a test for this string includes many this';$replace='/this/e';function userdefine()
    {
        static $i;
        $i=$i+1;
        return "that($i)";
    }
    $result=preg_replace(
            $replace,
            "userdefine()",
            $str
        );echo $result,"\n";?>
    [/code]
      

  5.   

    <?php
    $str='this is a test for this string includes many this';$replace='/this/e';function userdefine()
    {
        static $i;
        $i=$i+1;
        return "that($i)";
    }
    $result=preg_replace(
            $replace,
            "userdefine()",
            $str
        );echo $result,"\n";?>
      

  6.   

    4楼正解
    preg_replace_callback(PHP 4 >= 4.0.5)
    preg_replace_callback -- 用回调函数执行正则表达式的搜索和替换