note 118571 added to function.str-replace
| From: | kkumar dot sandeep89 at gmail dot com | Date: | Wed, 30 Dec 2015 09:33:23 +0000 |
| Subject: | note 118571 added to function.str-replace | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-204567@lists.php.net to get a copy of this message | ||
I came across requirement to replace some occurrences of a letter with other letter in a word:
<?php
$a = 'ctl00_ContentPlaceHolder1_Rpt_data_ctl01_lnk_name';
$b = 'ctl00$ContentPlaceHolder1$Rpt_data$ctl01$lnk_name';
echo "db has $a\n";
echo "we want $b\n";
//Make replacement array
$replacement = array(
array('_','$'),
array('_','$'),
array('_','_'),
array('_','$'),
array('_','$'),
array('_','$'),
);
//call function
echo replaceByArray($a,$replacement);
function replaceByArray($a, $replacement){
$j=0;
for($i=0; $i<strlen($a); $i++){
if($a[$i]==$replacement[$j][0]){
$a[$i] = $replacement[$j][1]; //make replacement
$j++; //increment index of replacement array
if($j == (count($replacement)-1)) break; //break out of loop if replacement array processed
}
}
return $a;
}
?>
----
Server IP: 72.52.91.14
Probable Submitter: 182.71.177.121
----
Manual Page -- http://php.net/manual/en/function.str-replace.php
Edit -- https://master.php.net/note/edit/118571
Del: integrated -- https://master.php.net/note/delete/118571/integrated
Del: useless -- https://master.php.net/note/delete/118571/useless
Del: bad code -- https://master.php.net/note/delete/118571/bad+code
Del: spam -- https://master.php.net/note/delete/118571/spam
Del: non-english -- https://master.php.net/note/delete/118571/non-english
Del: in docs -- https://master.php.net/note/delete/118571/in+docs
Del: other reasons-- https://master.php.net/note/delete/118571
Reject -- https://master.php.net/note/reject/118571
Search -- https://master.php.net/manage/user-notes.php