hello, this is my first post I think... it deals with the order of replacement, in the preg_replace() function, when you're replacing multiple values. Kind of hard to explain... but here is an example...

 
$count = 0;
$text = "in out in out";
$pattern = array('/in/e','/out/e');
$replacement = array('in()','out()');
$return = preg_replace($pattern,$replacement,$text);

function in(){
global $count;
$count++;
echo "IN: ".$count;
return "in: ".1;
}


function out(){
global $count;
$count--;
echo "OUT: ".$count;
return "out: ".0;
}

echo $return;

Ok, I made the code simpler, so you guys can see how it works... now this is the result...

IN: 1
IN: 2
OUT: 1
OUT: 0

in: 1
out: 0
in: 1
out: 0

Notice the $count, how it goes 1-2-1-0... thats because it searches $text for "in" first... than goes back and searches $text for "out"... It doesn't go line by line? Is there anyway (like a modifier) to make PREG searches for everything one time through? Instead of going one $pattern at a time?

My desired result would be:

IN: 1
OUT: 0
IN: 1
OUT: 0

in: 1
out: 0
in: 1
out: 0

Any input would be good. Let me know if you need me to explain more...

    Unfortunately this is how preg_replace works with arrays, it treats each element of the array as a completely seperate call. It's really just like doing

    for($i=0;$i<count($seach);$i++) {
      preg_replace($search[$i],$replace[$i],$subject);
    }
    

    but without the overhead.
    HTH
    Bubble

      Originally posted by bubblenut
      Unfortunately this is how preg_replace works with arrays, it treats each element of the array as a completely seperate call. It's really just like doing

      for($i=0;$i<count($seach);$i++) {
        preg_replace($search[$i],$replace[$i],$subject);
      }
      

      but without the overhead.
      HTH
      Bubble [/B]

      Oh no thats what i was afraid of... HTH Bubble? What's that? Thanks for the quick reply...

      Oh no... so now i have to think about a work around... <-- puts on thinking cap.

        Hows about something like this?

        $count=0;
        function inout($input) {
          global $count;
          switch($input[0]) {
            case 'in':
              $count++;
              echo('IN: '.$count);
              return 'in: 1';
              break;
            case 'out':
              $count--;
              echo('OUT: '.$count);
              return 'out: 0';
              break;
          }
        }
        
        $string='in out in out';
        echo(preg_replace_callback('/in|out/','inout',$string));
        

        I haven't tested it so it'll probably need tweaking but the logic should be sound. Lut us know how you got on with it.
        HTH
        Bubble

          Originally posted by bubblenut
          Hows about something like this?

          $count=0;
          function inout($input) {
            global $count;
            switch($input[0]) {
              case 'in':
                $count++;
                echo('IN: '.$count);
                return 'in: 1';
                break;
              case 'out':
                $count--;
                echo('OUT: '.$count);
                return 'out: 0';
                break;
            }
          }
          
          $string='in out in out';
          echo(preg_replace_callback('/in|out/','inout',$string));
          

          I haven't tested it so it'll probably need tweaking but the logic should be sound. Lut us know how you got on with it.
          HTH
          Bubble [/B]

          hey..... that's a good idea... nice... yeh... yeh, that'll work

          that concept will work... thanks bubbles.

          Hopefully I can use it... because this one little part is just a little part in this thing i'm trying to script... It'll have to be a crazy Reg. Exp. Pattern, cause... much more complicated than In|Out... lol

          Good job, I didn't think of this.

            Oh gimme gimme gimme <drool> ... bubble like regex πŸ˜ƒ

              Originally posted by bubblenut
              Oh gimme gimme gimme <drool> ... bubble like regex πŸ˜ƒ

              That's ok... I think i'm going to attack this problem in a different way... Thanks anyways bubbles.

                Write a Reply...