note 102996 deleted from function.str-replace by danbrown

From: Date: Sat, 19 Mar 2011 15:25:27 +0000
Subject: note 102996 deleted from function.str-replace by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-178013@lists.php.net to get a copy of this message
Note Submitter: ayalsule at gmail dot com ---- Alternative solution <?php function Replacment(&$paragraph,$oldWord,$newWord){ //check the inputs if (is_string($paragraph) && is_string($oldWord) && is_string($newWord)){ //search if word exist in paragraph $replace_count = 0; while (stripos($paragraph, $oldWord) !== false) { $find = stripos($paragraph, $oldWord); // split the paragraphe to two string (before the word and after the word) $str1 = substr($paragraph,0,$find); $start_cut = $find + strlen($oldWord); $str2 = substr($paragraph,$start_cut); //Re-merge the string with the new word $paragraph = $str1.$newWord.$str2; $replace_count ++; } return $replace_count; }else{ return false; } } //Example $paragraph = 'Hi my Name is Ayham Alsuleman'; $oldWord = 'Ayham'; $newWord = 'Hemo'; print '<font color="red">Old Paragraph:</font>'.$paragraph; print '<br /><font color="red">Old Word:'.$oldWord.'</font> , New Word:'.$newWord; print '<br />The count of replacmet is'.Replacment(&$paragraph,$oldWord,$newWord); print '<br /><font color="green">New Paragraphe;</font>'.$paragraph; ?>

« previous php.notes (#178013) next »