Hi all.

I added a contribution to http://www.php.net/str_replace
See:
jeroen at remove dot anti dot spam dot all-stars dot nl
19-May-2004 07:34

I checked and doublechecked with the preview function, but still I
overlooked a typo.
At three locations, ![] is written, which should be []

I hope you would be so kind to correct that. I would appreciate that very
much.

For your convenience, I added the correct code to this e-mail as attachment.
If you replace the content of my contribution with the content of that
attachment, I would be much obliged.

Best regards,
Jeroen Buse.
Function str_replace_clean()
 /* 
 * The basis for this function comes from the contribution 
 * by Xavier at 30-Dec-2003.
 * I modified it so that all parameters can be arrays or strings,
 * and I added an option for case-sensitive comparison or not 
 * (standard function str_replace() compares case-sensitive).
 */ 
 public function str_replace_clean($search_in, $replace_in, $subject_in, 
                                   $cs=true, $subject_out=array())
 {
  if ($cs!true and $cs!false) return $subject_in;
  $search=$search_text=$replace=$replace_text=array();
  if (is_array($search_in)) {
   $search = $search_in;
  }
  elseif (is_string($search_in)) {
   $search[] = $search_in; //array_push doesn't work here
  }
  if (is_array($replace_in)) {
   $replace = $replace_in;
  }
  elseif (is_string($replace_in)) {
   //make an array with the length of array $search
   foreach ($search as $dummy)
   {
    $replace[] = $replace_in; //array_push doesn't work here
   }
  }
  
  if (is_array($subject_in))
  {
   foreach ($subject_in as $subject)
   {
    $resultsubject = $this->do_while_for_str_replace_clean($search, $replace, 
                                                 $subject, $cs, $subject_out);
    $subject_out[] = $resultsubject; //array_push doesn't work here
   }
   return $subject_out;
  }
  elseif (is_string($subject_in))
  {
   $subject=$subject_in;
   $resultsubject = $this->do_while_for_str_replace_clean($search, $replace, 
                                                $subject, $cs, $subject_out);
   return $resultsubject;  
  } 
 }


 /* 
 * Private function do_while_for_str_replace_clean()
 * See public function str_replace_clean()
 */  
 private function do_while_for_str_replace_clean($search, $replace, 
                                        $subject, $cs, $subject_out)
 {
  while (count($search))
  {
   // get current terms, reduce the arrays for the next iteration
   $search_text = array_shift($search);
   $replace_text = array_shift($replace);
   // check if the substring is present
   if ($cs===true)
   {
    $pos = strpos($subject, $search_text);
    $break=$search_text;
   }
   else
   {
    $pos = strpos(strtolower($subject), strtolower($search_text));
    if (is_int($pos))
    {
     //replace all matches with their lowercase values for later explode()
     $len=strlen($search_text);
     $subject_len=strlen($subject);
     for ($pos2=$pos; ($pos2+$len)<=$subject_len ; $pos2=$newpos)
     {
      $subject=substr($subject,0,$pos2)
              .strtolower($search_text)
        .substr($subject,$pos2+$len);
      $newpos = strpos(strtolower($subject), strtolower($search_text),$pos2+$len);
      if (!is_int($newpos)) break;
     }
    }    
    $break=strtolower($search_text);
   }
       
   if (is_int($pos))
   {
    // match found - break in pieces
    $pieces = explode($break, $subject);
    if (count($search)) // only if there are more substitutions to do
    {
     // make next substitutions in every piece of text between matches
     foreach ($pieces as $k => $v)
     {
      if (strlen($v)) $pieces[$k] = $this->str_replace_clean($search, $replace, 
                                                         $v, $cs, $subject_out);
     }
    }
    $subject = join($replace_text, $pieces);
    break;
   }
  } //end while
  return $subject;
 }

--------------------------------------------------------------------

The functions are written in an object-ori�nted style, and tested with PHP5
(Release Candidate 2). 

If you do not want to use these functions as methods in a class, 
remove all " $this-> " inside the functions, and remove "public" and "private" 
before "function". 

Reply via email to