note 40778 deleted from function.str-replace by felipe

From: Date: Sun, 06 Jan 2008 12:49:31 +0000
Subject: note 40778 deleted from function.str-replace by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-134883@lists.php.net to get a copy of this message
Note Submitter: Darren Gates - http://www.tufat.com ---- Here's a version of str_replace which ignores differences in spacing (like tab, carriage return, line feed, etc.) in the two input strings. Courtesy of http://www.tufat.com (Darren's Script Archive). <?php function str_rep($srcfor,$chwith,$mystr) { if(strlen($mystr) == 0) return ""; $tok = strtok($srcfor," \n\t\r"); $strsrc = array(); $idxsrc = array(); $i = 0; while($tok) { $strsrc[$i] = $tok; $pos = strpos($srcfor,$tok); $idxsrc[$i]=$pos; $tok = strtok(" \n\r\t"); $i++; } $tok2 = strtok($mystr," \n\t\r"); $str = array(); $idx = array(); $j = 0; while($tok2) { $str[$j] = $tok2; $pos = strpos($mystr,$tok2); $idx[$j]=$pos; $tok2 = strtok(" \n\r\t"); $j++; } for($m=0;$m<$j;$m++) { if(strcasecmp($strsrc[0] , $str[$m]) == 0) { for($l=1;$l<$i;$l++) { if(strcasecmp($strsrc[$l],$str[$m+$l]) != 0) break; } $l--; if(($l+1) == $i) { $new_str=substr($mystr,0,$idx[$m]); $new_str .= $chwith; $index = $idx[$m+$l]+strlen($str[$l+$m]); $len = strlen($mystr)-$index; if($len > 0) $new_str .= str_rep($srcfor,$chwith,substr($mystr,$index,$len)); return $new_str; break; } } } return $mystr; } ?>

« previous php.notes (#134883) next »