note 102996 added to function.str-replace
| From: | ayalsule at gmail dot com | Date: | Sat, 19 Mar 2011 05:40:26 +0000 |
| Subject: | note 102996 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-177997@lists.php.net to get a copy of this message | ||
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;
?>
----
Server IP: 69.147.83.197
Probable Submitter: 82.137.200.9
----
Manual Page -- http://www.php.net/manual/en/function.str-replace.php
Edit -- https://master.php.net/note/edit/102996
Del: integrated -- https://master.php.net/note/delete/102996/integrated
Del: useless -- https://master.php.net/note/delete/102996/useless
Del: bad code -- https://master.php.net/note/delete/102996/bad+code
Del: spam -- https://master.php.net/note/delete/102996/spam
Del: non-english -- https://master.php.net/note/delete/102996/non-english
Del: in docs -- https://master.php.net/note/delete/102996/in+docs
Del: other reasons-- https://master.php.net/note/delete/102996
Reject -- https://master.php.net/note/reject/102996
Search -- https://master.php.net/manage/user-notes.php