I need to sum two utf-8 bangla number . Here is the code :
<?php
$a = ১২; //(12)
$b = ৫; //(5)
echo $c = $a + $b; //OUTPUR 17
?>
I need the outpur but now its show 0 so how can I do it ? Advance thanks to all
You can't just simply do it like that, you need to convert them to normal digits first:
class Converter
{
public static $bn = ["১", "২", "৩", "৪", "৫", "৬", "৭", "৮", "৯", "০"];
public static $en = ["1", "2", "3", "4", "5", "6", "7", "8", "9", "0"];
public static function bn2en($number)
{
return str_replace(self::$bn, self::$en, $number);
}
public static function en2bn($number)
{
return str_replace(self::$en, self::$bn, $number);
}
}
$a = '১২'; //(12)
$b = '৫'; //(5)
$c = Converter::bn2en($a) + Converter::bn2en($b); // $c = 17
echo Converter::en2bn($c); // ১৭
Credit to here: http://bits.mdminhazulhaque.io/php/convert-number-between-banlga-and-english-in-php.html
another approach by using intl
extension :
// create a format from ba local
// you can get all available locales by : print_r(IntlCalendar::getAvailableLocales());
$format = numfmt_create('ba', NumberFormatter::DECIMAL);
$a = numfmt_parse($format, '১২');
$b = numfmt_parse($format, '৫');
echo $c = $a + $b;
Output : 17
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With