Thursday, April 28, 2011

Using ereg_replace in PHP?

How do I replace using the following code?

ereg_replace("%Data_Index\[.\]%", $this->resultGData[$key ][\\1], $var)

I want to replace the number in [] %Data_Index
to $this->resultGData[$key ][\\1] same %Data_Index
and how ex %Data_Index[1] = $this->resultGData[$key][1], $var);

replace number in %Data_Index[...........] in []
to $this->resultGData[$key ][............] same number

From stackoverflow
  • hello monkey_boys

    your question is a little bit hard to understand

    the smartest way to replace what you are asking I believe would be using a cycle

    for example if you know that $this->resultGData[$key ][] has 10 elements on them you could simply do this, asuming %Data_Index[1] (are you sure it isn't $Data_Index? i'll asume that) you can try the following

    $total = count($this->resultGData[$key ]);  //we get the total of elements in that key
    
    for($i=0;$i<$total;$i++)
    {
      $Data_Index[$i] = $this->resultGData[$key][$i];
    }
    

    now if the $key changes, you'd need to do this for every $key :)

    keep practicing your english, it's a really useful tool in the IT field :) (not that i'm very a good at it either :P)

    monkey_boys : no that string not array
  • Try the preg_replace() function with the e modifier instead:

    preg_replace('/%Data_Index\[(\d+)\]%/e', '$this->resultGData[$key][\1]', $var);
    

    Note that this function uses Perl-compatible regular expressions instead of POSIX-extended regular expression.

    monkey_boys : it work great thank

0 comments:

Post a Comment

Note: Only a member of this blog may post a comment.