Ok so here is what I'm trying to do:

Given a string, say "This is a test of a string", I want to have an array ("first","second") and from that array, I'm going to do a search for "a" and use one of the values out of the array to replace "a" with. So, if I run this function, my resulting string is:

"This is first test of second string".

So, I started from a function that was on php.net called eregi_array_replace. Here is the function:

    function eregi_array_replace($pattern,$string,$replace_array)
    {
      for($x = 0;eregi($pattern,$string,$match);$x++)
      {
       $current_match = $match[0];
       $start = strpos($string,$current_match);
       $length = strlen($current_match);
       $substring = substr($string,$start,$length);

   if(@!$replace_array[$x]) $x = 0;
   $substring = eregi_replace($pattern,$replace_array[$x],$substring);

   $start = strpos($string,$current_match,$start);
   $string = substr_replace($string,$substring,$start,$length);
  }
  return $string;
}    

This function works great, but the problem is I really need word boundary support. So, what I wanted to do is convert this to a preg_array_replace. So, I tried this:

    function preg_array_replace($pattern,$string,$replace_array)
    {
      for($x = 0;preg_match($pattern,$string,$match);$x++)
      {
       $current_match = $match[0];      
$start = strpos($string,$current_match); $length = strlen($current_match); $substring = substr($string,$start,$length); if(@!$replace_array[$x]) { print $x . "\n"; $x = 0; } $substring = preg_replace($pattern,$replace_array[$x],$substring); $start = strpos($string,$current_match,$start); $string = substr_replace($string,$substring,$start,$length); } return $string; }

So what's the problem? It doesn't work I'm suspecting it has something to do with the return result from the invocaton of preg_match versus eregi. But, here's an example in code:

  $word_pattern = "/\ba\b/i";
  $testArray = array('an','or','the');
  $buffer = "This is a test of a string can it work.";

  print preg_array_replace($word_pattern, $buffer, $testArray); 

You'll notice if you run this, the return is going to look weird because for some reason the "a" inside of "can" is getting replaced. If I do something similar with the ereg version:

  $word_pattern = " a ";
  $testArray = array('an','or','the');
  $buffer = "This is a test of a string can it work.";

  print eregi_array_replace($word_pattern, $buffer, $testArray);

It's going to return a fine result. But, I don't want to pad with spaces as this defeats the purpose of the word boundary.

After some further research today I did discover it definitely has to do with the way eregi return the matches versus preg_match. With eregi, you actually get the strpos the match was found at and there can perform a replace based on this. I can't figure out an ingenious way (yet) to accomplish this same thing with preg.

Thanks in advance.

    Being picky, I'd even complain about the order that arguments are supplied to the function. $pattern, $replacement_array, $string would seem more appropriate... Meh.

    The code seems spectacularly overcomplicated, but that's probably because it's trying to force eregi_replace() to work despite its limited functionality. I had a look at the copy on php.net's site to see what it was supposed to do, and it looks like...

    function preg_array_replace($pattern, $string, $replacement_array)
    {
    	$ra_size = count($replacement_array);
    	if($ra_size==0) return $string;
    	$ra_index=0;
    	while(preg_match($pattern,$string))
    	{
    		$string = preg_replace($pattern, $replacement_array[$ra_index], $string, 1);
    		$ra_index = ($ra_index+1)%$ra_size;
    	}
    	return $string;
    }
    

    ereg_replace() doesn't have that fourth parameter.

      Write a Reply...