I want to fetch input from view file and split that number into digits and replace each digit to a specific alphabet and then store it in database,
can anyone help me in this problem
$data = [
'product_id' => $productId,
'vendor_id' => $vendor_id,
'purchase_date' => $purchase_date,
'landing_price' => $landing_price,
'retail_price' => $retail_price,
'wholesale_price' => $wholesale_price,
'current_price' => $current_price,
'vendor_current_price' => $vendor_current_price,
'freight' => $freight
];
$numbers = str_split($wholesale_price);
$replaceNumbers = [
1 => 'S',
2 => 'H',
3 => 'O',
4 => 'N',
5 => 'E',
6 => 'L',
7 => 'A',
8 => 'P',
9 => 'U',
0 => 'R'
];
$replace = str_replace($numbers, $replaceNumbers, $numbers);
$JoinReplacedWord = join($replace);
var_dump($numbers, $replace, $JoinReplacedWord);
die;
but i am not getting the number replaced i am getting the array replaced to alphabet
result –
array(4) {
[0]=> string(1) "1"
[1]=> string(1) "4"
[2]=> string(1) "5"
[3]=> string(1) "0"
}
array(4) {
[0]=> string(1) "S"
[1]=> string(1) "H"
[2]=> string(1) "O"
[3]=> string(1) "N"
}
string(4) "SHON"
3
Answers
use array_replace instead str_replace
EDIT : bug fix for repeated digit example 88775
I try to dig into this. According to your description, your code does exactly what you want it to do. It splits up
$data['wholesale_price']
and has each character replaced as defined by$replaceNumbers
. According to your output, your input number was ‘1450’.Do you mean by "i am not getting the number replaced" that
$data
is not refreshed? Then simply add$data['wholesale_price'] = $JoinReplacedWord;
.If you just define your
$replaceNumbers
array slightly different your code will workRESULT