note 118571 deleted from function.str-replace by cmb
| From: | cmb@php.net | Date: | Sun, 15 Sep 2019 14:52:06 +0000 |
| Subject: | note 118571 deleted from function.str-replace by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-212219@lists.php.net to get a copy of this message | ||
Note Submitter: kkumar dot sandeep89 at gmail dot com
----
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;
}
?>