本帖最后由 weweidisid 于 2011-10-12 22:22:43 编辑

解决方案 »

  1.   

    没有现成的函数,可以自己写。
    随手写个简单的,没找到更好的。
    1.<?php
      header("content-type:text/html;charset=utf-8");
      $str = "a正对个地方a的感大概大师傅a似的但是a豆腐干似的a";
      $startIndex = strpos($str,'a');
      $str{$startIndex}="`";
      
      $index2 = strpos($str,'a');
      $str{$index2} ='b';
      
      $index3 = strpos($str,'a');
      $str{$index3} = "`";
      
      $index4 = strpos($str,'a');
      $str{$index4} ='c';
     
      $str = str_replace('`','a',$str);
      echo $str;
      

  2.   

    可以用正则
    $preg= '/(.*?a.*?)a(.*?a.*a)/';
    $str = "a反对个地方a的感大概大师傅a似的但是a豆腐干似的a";
    preg_replace($preg,$1b$2c,$str);
    echo $str;
      

  3.   

    在问个数据库的事啊
    比如我有10条数据删除最后2条后然后在添加数据主键值变成10了
    怎么把记录删干净让他重新重第8条开始啊?
    INSERT 插入不要啊,我要删干净
      

  4.   

    括号弄错了
    $preg= '/(.*?a.*?)a(.*?a.*?)a/';
    $str = "a反对个地方a的感大概大师傅a似的但是a豆腐干似的a";
    preg_replace($preg,$1b$2c,$str);
    echo $str;
      

  5.   

    $str = 'a反对个地方a的感大概大师傅a似的但是a豆腐干似的a';function foo($char){
    static $i = 0;
    $t = range('a', 'z');
    $res = $t[$i];
    $i++;
    return $res; 
    }
    echo preg_replace('/(a)/e', 'foo(\\1)', $str);//output a反对个地方b的感大概大师傅c似的但是d豆腐干似的e
      

  6.   

    可用alter table table_name AUTO_INCREMENT=n命令来重设自增的起始值。
      

  7.   

    上面没将代码格式化,看着别扭的很,另外加一个参数更符合LZ个性。$str = 'a反对个地方a的感大概大师傅a似的但是a豆腐干似的a';function foo($char){
    static $i = 0;
    $t = range('a', 'z');
    $res = $t[$i];
    $i++;
    return $res; 
    }
    echo preg_replace('/(a)/e', 'foo(\\1)', $str,2);//此处设置改变个数//output a反对个地方b的感大概大师傅c似的但是d豆腐干似的e
      

  8.   


    ALTER TABLE table_name AUTO_INCREMENT=n //小写是变量,table_name为表名,n为你想设置的起始值
      

  9.   

    $str = "a反对个地方a的感大概大师傅a似的但是a豆腐干似的a";
    $ar = explode("a", $str);
    $str = $ar[0];
    for(var $i=1;$i<count($ar); $i++)
    {
       $c = "a";
       if($i==1) $c="b";
       if($i==3) $c="c";
       $str.=$c.$ar[$i];
    }
      

  10.   

    报错啊!!Notice: Use of undefined constant a - assumed 'a' in E:\wamp\www\file.php(11) : regexp code on line 1Notice: Use of undefined constant a - assumed 'a' in E:\wamp\www\file.php(11) : regexp code on line 1
    a反对个地方b的感大概大师傅a似的但是a豆腐干似的a
      

  11.   

    preg_replace很强大,结合正则可以实现任何替换。
      

  12.   

    $str = 'a反对个地方a的感大概大师傅a似的但是a豆腐干似的a';function foo($char){
        static $i = 0;
        $t = range('a', 'z');
        $res = $t[$i];
        $i++;
        return $res; 
    }
    echo preg_replace('/(a)/e', 'foo("\\1")', $str,2);//此处设置改变个数,此处加变量引号.
      

  13.   


    昨天睡觉太早了,都没看到这么多回复了。还想到一个explode的,看了下,跟十楼差不多。<?php
    $str = "a反对个地方a的感大概大师傅a似的但是a豆腐干似的a";
    $ar = explode("a", $str);
    $str = $ar[0];
    for($i=1;$i<count($ar);$i++)
    {
       $char = ($i==2?'b':($i==4?'c':'a'));
       $str.=$char.$ar[$i];
    }
    echo $str;
      

  14.   

    $s = "a反对个地方a的感大概大师傅a似的但是a豆腐干似的a";function foo($r) {
      global $num;
      $num++;
      if($num == 2) return 'b';
      if($num == 4) return 'c';
      return $r[0];
    }echo preg_replace_callback('/a/', 'foo', $s);a反对个地方b的感大概大师傅a似的但是c豆腐干似的a
      

  15.   

    再来个另类的$val = 'a反对个地方a的感大概大师傅a似的但是a豆腐干似的a';
    preg_match_all('/a/',$val,$matchs);
    for($i = 1,$m = 1 ; $i <= count($matchs[0]) ; $i++){
    if($i==2){
    $val = preg_replace('/a/','%'.(++$m).'$s',$val,1);
    }elseif($i==4){
    $val = preg_replace('/a/','%'.(++$m).'$s',$val,1);
    }else{
    $val = preg_replace('/a/','%1$s',$val,1);
    }
    }
    echo $val,"<br />\n";
    printf($val,'a','b','c');
      

  16.   


    function user_str_replace($s, $p){
    $end = strrpos($s, 'a');
    if($end === false){
    return $s;
    }
    $count = 0;
    $index = 0;
    while(($index = strpos($s, 'a', $index)) !== false){
    ++$count;
    ++$index;
    foreach($p as $k => $v){
    if($count == $k){
    list($replace, $txt) = each($v);
    $s = substr($s, 0, $index - 1) . $txt . substr($s, $index + strlen($replace) - 1);
    // goto P;
    }
    }
    // P:
    }
    return $s;
    }//$s = 'la反对个地方a的感大概大师傅a似的但是a豆腐干似的a';
    $s = '----a----a----a----a----a----';
    $p = array(
    2 => array('a' => 'b'),
    4 => array('a' => 'c'),
    );
    echo user_str_replace($s, $p);
      

  17.   

    你的这个不行<?php
    header("content-type:text/html;charset=utf-8");
    $str = "a反对个地方a的感大概大师傅a似的但是a豆腐干似的a";
    $preg= '/(.*?a.*?)a(.*?a.*?)a/';
    $replacement = "\$1b\$2c";
    print preg_replace($preg, $replacement, $str);