How to sum two Bangla numbers (UTF-8) in PHP?

I need to sum two utf-8 bangla values. Here is the code:

<?php 
$a = ১২; //(12)
$b = ৫; //(5)
echo $c = $a + $b; //OUTPUR 17
?>

      

I need acceleration but now it is showing 0, so how can I do that? Thanks to all

+3


source to share


2 answers


You can't just do it like this, you need to convert them to regular numbers 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 here: http://bits.mdminhazulhaque.io/php/convert-number-between-banlga-and-english-in-php.html

+2


source


another approach using an 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

+2


source







All Articles