Home > Software design >  Replace a number input to alphabetical string in Codeignitor 4
Replace a number input to alphabetical string in Codeignitor 4

Time:12-20

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"

CodePudding user response:

use array_replace instead str_replace

    $wholesale_price = 98765;

    $numbers = array_flip(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 = array_slice(array_replace($numbers, $replaceNumbers), 0, count($numbers));

    $JoinReplacedWord = join($replace);
    var_dump($JoinReplacedWord);
    //you will see string(5) "UPALE"
    die;

CodePudding user response:

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;.

CodePudding user response:

If you just define your $replaceNumbers array slightly different your code will work

$data = [
    'product_id' => 1,
    'vendor_id' => 2,
    'purchase_date' => '2021-12-12',
    'landing_price' => 123.45,
    'retail_price' => 234.56,
    'wholesale_price' => 12345678.90,
    'current_price' => 456.78,
    'vendor_current_price' => 567.89,
    'freight' => 111.22
];

$replaceNumbers = ['R', 'S', 'H', 'O', 'N', 'E', 'L', 'A', 'P', 'U'];

$numbers = str_split($data['wholesale_price']);

$replace = str_replace($numbers, $replaceNumbers, $numbers);

$JoinReplacedWord = join($replace);
var_dump($numbers, $replace, $JoinReplacedWord);

RESULT

RSHONELAPU
  •  Tags:  
  • php
  • Related