note 53069 deleted from function.str-replace by bjori

From: Date: Sun, 26 Mar 2006 22:23:27 +0000
Subject: note 53069 deleted from function.str-replace by bjori
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-106671@lists.php.net to get a copy of this message
Note Submitter: spectrereturns at creaturestoke dot com ---- This is a little function I wrote replaces substrings in a string with different values. If there are more instances of $search in $string then there are elements in $replace, it will start over. <?php function replace_different($search,$replace,$string) { $occs = substr_count($string,$search); $last = 0; $cur = 0; $data = ''; for ($i=0;$i<$occs;$i++) { $find = strpos($string,$search,$last); $data .= substr($string,$last,$find-$last).$replace[$cur]; $last = $find+strlen($search); if (++$cur == count($replace)) { $cur = 0; } } return $data.substr($string,$last); } ?> Example: <?php $string = 'atext,btext'; echo replace_different('',array('0~','1~`'),$string); ?> Will return: 0~atext1~,0~btext1~

« previous php.notes (#106671) next »