All pastes #342778 Raw Edit

Stuff

public text v1 · immutable
·published 2007-02-06 15:30 UTC
rendered paste body
HOW DO I TWEAK THAT FUNCTION so instad of replace the only first match, it will replace FROM the first match?


function str_replace_count($search,$replace,$subject,$times) {

   $subject_original=$subject;
   
   $len=strlen($search);    
   $pos=0;
   for ($i=1;$i<=$times;$i++) {
       $pos=strpos($subject,$search,$pos);
       
       if($pos!==false) {                
           $subject=substr($subject_original,0,$pos);
           $subject.=$replace;
           $subject.=substr($subject_original,$pos+$len);
           $subject_original=$subject;
       } else {
           break;
       }
   }


$w="abracadabra";

print str_replace_count("abra","----",$w,2);
   
   return($subject);

}