Converting a price to a string with a specific string code in PHP

I need help, at first, perhaps this is a duplicate, but I think that I’m already looking and did not find anything, as I expected and wanted.

I am confused about my problem. I wanted to convert the number format to string format,

I have a line called

$pricecode = 'ABCDEFGHIJ';

sort of:

ABCDEFGHIJ for number 1234567890

for a number from 0 to 9 and an example of the type:

$price = 1500;

so I need to replace the price and become:

AEJJ

I am trying to create code as below:

$priceCode = 'ABCDEFGHIJ';
$price = 1500;
$lenPrice = strlen($price);
$priceconveert = '';
$i=1;
$x=0;
while($i<=$lenPrice){
    $number = substr($Price,$x,1);                          
    if($number == 1){
        $priceconveert .= substr($priceCode,$x,1);                                        
    }else if($number == 2){
        $priceconveert .= substr($priceCode,$x,1);                                        
    }else if($number == 3){
        $priceconveert .= substr($priceCode,$x,1);                                        
    }else if($number == 4){
        $priceconveert .= substr($priceCode,$x,1);                                        
    }else if($number == 5){
        $priceconveert .= substr($priceCode,$x,1);                                        
    }else if($number == 6){
        $priceconveert .= substr($priceCode,$x,1);                                        
    }else if($number == 7){
        $priceconveert .= substr($priceCode,$x,1);                                        
    }else if($number == 8){
        $priceconveert .= substr($priceCode,$x,1);                                        
    }else if($number == 9){
        $priceconveert .= substr($priceCode,$x,1);                                        
    }else if($number == 0){
        $priceconveert .= substr($priceCode,$x,1);                                        
    }
    $i++;
    $x++;
}

echo $priceconveert;

With a code like a cloister, there should be a result

AEJJ

, . AEJJ, , , AEJ2, 1500 2 (00), 00 - JJ, make short J2, 15000 J3, 3 (000).

, , , , , numeric 1500 into 4 array priceCode into 10 array. .

, , , . . .

+4
5

,

$letters = array('A', 'B','C','D','E','F','G','H','I','J');
$numbers   = array('1', '2','3','4','5','6','7','8','9','0');
$text    = '1500';
$output  = str_replace($numbers,$letters,  $text);
echo $output;

AEJJ

DeEMO

$letters = array('A', 'B','C','D','E','F','G','H','I','J');
$numbers   = array('1', '2','3','4','5','6','7','8','9','0');
$text    = 'AEJJ';
$output  = str_replace($letters,$numbers,  $text);
echo $output;

1500

DEMO

+5

( ) AEJJ AEJ2

$string = 'AEJJ';
function howmanytimes($str,$char){
    $maxcount=0;
    $thiscount=0;
    for($i=0;$i<strlen($str);$i++){
        if(substr($str,$i,1)==$char){
            $thiscount++;
            if($thiscount>$maxcount) $maxcount=$thiscount;
        }else $thiscount=0;
    }
    return $maxcount;
}
$cnt = howmanytimes($string,'J');

if ($cnt > 1) {
    $r_string = '';
    for ($i = $cnt; $i > 0; $i--) { 
        $r_string .= 'J';
    }
    $n_string = 'J'.$cnt;
    echo str_replace($r_string, $n_string, $string);
} else {
    echo $string;
}

,

+1

Use preg_split()and try


Text in price

$priceCode = 'ABCDEFGHIJ';
$chars = preg_split('//', $priceCode, -1, PREG_SPLIT_NO_EMPTY);

$price = '';

foreach ($chars as $value) {

    switch ($value) {
        case 'A':
            $price .=   1;
            break;
        case 'B':
            $price .=   2;
            break;
        case 'C':
            $price .=   3;
            break;
        case 'D':
            $price .=   4;
            break;
        case 'E':
            $price .=   5;
            break;
        case 'F':
            $price .=   6;
            break;
        case 'G':
            $price .=   7;
            break;
        case 'H':
            $price .=   8;
            break;
        case 'I':
            $price .=   9;
            break;
        case 'J':
            $price .=   0;
            break;

        default:
            # code...
        break;
    }

}
echo($price);

Output 1234567890

phpfiddle preview


Number to text

$priceCode      = array('A', 'B','C','D','E','F','G','H','I','J');
$number         = array('1', '2','3','4','5','6','7','8','9','0');
$price          = '19855';
$price_to_text  = str_replace($number,$priceCode,$price);
echo $price_to_text;

View Phpfiddle

0
source

Try entering the code:

$priceCodeVal = array('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J');
$priceCode = array('1', '2', '3', '4', '5', '6', '7', '8', '9', '0');
$price = '1500';
$priceconveert = str_replace($priceCode, $priceCodeVal, $price);
echo $priceconveert;

OR

$price = 1500;
$lenPrice = strlen($price);
$priceconveert = '';
$i = 1;
$x = 0;
while ($i <= $lenPrice) {
    echo $number = substr($price, $x, 1)." == ";
    if ($number == 1) {
        $priceconveert .='A';
    } else if ($number == 2) {
        $priceconveert .= 'B';
    } else if ($number == 3) {
        $priceconveert .= 'C';
    } else if ($number == 4) {
        $priceconveert .= 'D';
    } else if ($number == 5) {
        $priceconveert .= 'E';
    } else if ($number == 6) {
        $priceconveert .= 'F';
    } else if ($number == 7) {
        $priceconveert .= 'G';
    } else if ($number == 8) {
        $priceconveert .= 'H';
    } else if ($number == 9) {
        $priceconveert .= 'I';
    } else if ($number == 0) {
        $priceconveert .= 'J';
    } else {
        $priceconveert .= $number;
    }
    $i++;
    $x++;
}

echo $priceconveert;
0
source
$pricecode = ['J','A','B','C','D','E','F','G','H','I'];
$price = 1500;
$convert = str_replace(array_keys($price_code), $price_code, $price);
echo $convert;
0
source

Source: https://habr.com/ru/post/1616151/


All Articles