Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

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

like image 789
Poth Hara Pothik Avatar asked May 30 '17 09:05

Poth Hara Pothik


2 Answers

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

like image 84
Enstage Avatar answered Oct 04 '22 03:10

Enstage


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

like image 30
hassan Avatar answered Oct 04 '22 04:10

hassan