Read Number to Khmer Text Using PHP

This tutorial will explain the technique of reading number (Latin or Khmer character) to Khmer Unicode text using PHP. This tutorial is useful for the website or web application that requires Khmer language and needs to display Khmer number as text by just inputting only the number (Latin or Khmer character). This function can also be used in receipt or invoice generating in web application where the amount/price may need to be written in Khmer text. Example, by putting the number 11989 or ១១៩៨៩, you will get the return string as “មួយមឿនមួយពាន់ប្រាំបួនរយប៉ែតសិបប្រាំបួន” or “ដប់មួយពាន់ប្រាំបួនរយប៉ែតសិបប្រាំបួន”. To see how it works, let’s go through the tutorial.

INSTRUCTION

PHP Script

function num2khtext($complete_char,$enableThousand){
//function for split uft8 character
function mb_str_split( $string ) { 
//Split at all position not after the start: ^ 
//and not before the end: $ 
return preg_split('/(?<!^)(?!$)/u', $string ); 
}		
//remove left zeros
$cleanStr = ltrim($complete_char, '0');	
//split number/string to array
$num_arr = mb_str_split($cleanStr);	
$translated=''; $addThousand=false;
//string array
$khNUMTxt = array('','មួយ','ពីរ','បី','បួន','ប្រាំ');
$twoLetter = array('','ដប់','ម្ភៃ','សាមសិប','សែសិប','ហាសិប','ហុកសិប','ចិតសិប','ប៉ែតសិប','កៅសិប');
$khNUMLev = array('','','','រយ','ពាន់','មឿន','សែន','លាន');
$khnum = array('០','១','២','៣','៤','៥','៦','៧','៨','៩');
//loop to check each number character
foreach($num_arr as $key=>$value){
//convert khmer number to latin number if found
if(in_array($value,$khnum)){$value = array_search($value,$khnum);}
//allow only number
if(!is_numeric($value)){return '';}
//check what pos the charactor in
$pos = count($num_arr) - ($key);
if($pos>count($khNUMLev)-1){$pos=($pos % count($khNUMLev))+2;}
//enable or diable read in thousand
if($enableThousand and ($pos == 5 or $pos == 6)){$pos = $pos-3;}
//concatenate number as text
if($pos==2){
$translated .= $twoLetter[$value];
}else{
if($value>5){$translated .=  $khNUMTxt[5].$khNUMTxt[$value - 5];}else{$translated .= $khNUMTxt[$value];}
}
//work for thousand
if($pos==2 or $pos == 3 or $pos == 4){
if($value>0){$addThousand=true;}
}
//concatenate number level
if($value>0 or ($pos==4 and $addThousand and $enableThousand) or $pos==7){
$translated .= $khNUMLev[$pos];			
}
//make addthousand to default value (false)
if($pos==4){$addThousand=false;}		
}
//return the complete number in text
return $translated;
}

How to Use

echo ‘Read In Thousand: ’.num2khtext(“11989”,true);
echo ‘Not Read In Thousand: ’.num2khtext(“11989”,false);

Result

Read In Thousand: ដប់មួយពាន់ប្រាំបួនរយប៉ែតសិបប្រាំបួន
Not Read In Thousand: មួយមឿនមួយពាន់ប្រាំបួនរយប៉ែតសិបប្រាំបួន

Function Process

There are two parameters required by the function ($complete_char and $enableThousand).

  • $complete_char: full number string (number or string)
  • $enableThousand: enable or disable reading in thousand (boolean)

When function gets the number string, it splits the string into separated character and stores in an array. In this string splitting stage, we use customize function, mb_str_split, used to working with UTF8 character.

There are various text required for reading the number as we declare four arrays:

  • $khNUMTxt: stores Khmer number as reading text
  • $twoLetter: stores reading text for 10, 20, 30…
  • $khNUMLev: stores reading text for number level
  • $khnum: stores Khmer number

Now we have all the required variables, the next step is checking each character of the number string. In this checking stage, we need to check if there is number string in Latin string or Khmer Unicode string. If it’s in Khmer Unicode string, it needs to be converted to Latin string. And if the input string is not Latin string neither Khmer Unicode string, stop the function and then return blank.

If the input string is valid, we need to check where is this number character located in the whole string (index position). If we found the character index, we can reverse the index to get the real position in Khmer Reading Number Format. Then, we may need to check whether $enableThousand is enable or not. If it’s enable, the position index of that character must be justified to match the way we read for Khmer Number. After that, we can concatenate number text string one by one.

Finally, the last thing to do is identifying the number level whether it’s in hundred, thousand, million, etc. and then just return the complete read text. That’s it! Hope it’s helpful.

 

Similar Tutorials

Comments