I'm trying to get a replace going here. The string is something like this:

RE[2]: subject line, and I want to replace it with RE[3]: subject line.

I've tried 1000 different lines, but the closest I can get,

$subj = preg_replace("/[0-9]/","RE[$0]:",$subj); 

reproduces a whole chunk of the string. Can anyone give me a tip here?

Thanks,

    if it's only Re[2] -> Re[3] try

    $subj = preg_replace('%Re\\[2\\]%i','Re[3]',$subj);

    if you want Re[2] -> Re[3] and
    Re[3] -> R4] and
    Re[5464] -> Re[4565] and ...

    try

    $subj = preg_replace('%Re\\[([0-9]+)\\]%ie',"'Re['.($1+1).']'",$subj);

      I'm not sure if that's going to work, I think that the capture variable is only available within the preg. If it does work please post back and let us know but if it doesn't you may need to try something like this.

      preg_match('/Re\\[([0-9]+)\]/',$string,$matches);
      preg_replace('/Re\\[([0-9]+)\]/',++$matches[1],$string);
      

      HTH
      Bubble

        i think it should work since i use the 'e' modifier which evaluates the replace-string

        --
        edit:

        just tested it:
        code:

        echo $subj = 'Original:
        Re[2]
        Re[45]
        Re[54666]
        some text without re
        Re[a]
        
        Re[6
        ]
        
        end of text';
        echo 'after preg_replace:';
        echo $subj = preg_replace('%Re\\[([0-9]+)\\]%ie',"'Re['.($1+1).']'",$subj);

        output:

        Original:
        Re[2] Re[45] Re[54666] some text without re Re[a] Re[6 ] end of text
        
        after preg_replace:
        Re[3] Re[46] Re[54667] some text without re Re[a] Re[6 ] end of text

          Sweet!
          I never knew about the e modifier before, cheers MrHappiness!😃

            Write a Reply...