Reputation: 43
I need assist, first maybe this have duplicate but i think i already searching and nothing found as i expected and wanted.
I confuse about my problem. What i wanted is converting number format to string format,
I have string called
$pricecode = 'ABCDEFGHIJ';
something like :
ABCDEFGHIJ for number 1234567890
is for number from 0 to 9 and example like :
$price = 1500;
so i need price replacing and become :
AEJJ
Im trying to create code like 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 code like abode should be have result
AEJJ
but i have bad luck and result scrambled. Nothing like AEJJ and i would like if and can be done like AEJ2
because behind 1500
have 2 zero number (00) and price code for 00
is JJ
but make is short like J2
or if have price 15000
that would be J3 because have 3 zero (000).
I think if use array would be great, but i have no idea how to do it because about separate numeric 1500 into 4 array
and priceCode into 10 array
. I'm completely confuse.
Please help me, teach me how to resolve this. Thank You. I'm sorry for my language.
Thank you
Upvotes: 4
Views: 371
Reputation: 919
After getting the character string (for price) you can use the following to display AEJJ
like 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;
}
EDIT
See my code it will give you correct solution for string
Upvotes: 1
Reputation: 11971
$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;
Upvotes: 0
Reputation: 38652
Use preg_split()
and try this
Text to 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
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;
Upvotes: 0
Reputation: 2928
Try below 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;
Upvotes: 0
Reputation: 11987
Try this,
$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;
Output
AEJJ
To get back the number
$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;
Output
1500
Upvotes: 5