Learning Is Fun

Talks on Web Technology and Better Product Development

PHP & MySQL: Unicode number add, subtract etc for any language

December23

Hello Coders!

Recently a visitor from my country asked me how he can add Unicode numbers stored in MySQL database.  I was thinking that it is really a good question. Because you may store any number in any local language to your database and later you may try to add or delete them.

For example, in Bangla language an employee’s salary can be ২৪১৮৬ (which is 24186 in English) bucks and the coder may want to store it in a table under salary field. Another field can be bonus and it can be ২০০ (2000 in English). Now the coder may want to add them up anytime required. You know adding 24186 and 2000 is easy. But what the coder may do in case the values are stored in a local language?

The visitor asked me how he can use the MySQL sum() function in case the data are stored in Unicode format. Well. I do not think MySQL’s unicode will support the sum() function or even if there is one I do not know frankly speaking. So, I thought and developed an alternative solution.

Here is the alternative solution steps:

  1. I get the unicode numbers from the database
  2. Convert them to English number
  3. Sum them up or anything like substract, multiply or divide etc
  4. Convert the result to Unicode once again

Thus, it looks simple procedure. Right?

Before we start I would like to suggest you download the codes from the bottom link of the page and take a look. Because some code may not look as the original one for browser’s case sensitiveness. So, it is better to look at the original code from the download link below.

So,  I wrote a small class to handle the whole procedure and here goes the coding of my class:

{code type=PHP}

class UnicodeHandler
{
var $dbLink;
var $sqlQuery;
var $dbResult;
var $dbRow;
var $salary;
var $bonus;

/* Use this constructor in case your PHP version is 4. */
/*
function UnicodeHandler()
{
$this->dbLink = ”;
$this->sqlQuery = ”;
$this->dbResult = ”;
$this->dbRow = ”;
$this->mySalary = 0;
$this->myBonus = 0;

$this->dbLink = mysql_connect(‘localhost’, ‘root’, ”);
mysql_query(“SET character_set_results=utf8″, $this->dbLink);
mb_language(‘uni’);
mb_internal_encoding(‘UTF-8′);

mysql_select_db(‘test’, $this->dbLink);
mysql_query(“set names ‘utf8′”,$this->dbLink);
}
*/

/* Use this constructor in case your PHP version is 5 or 5+. */
/* I assume you are using PHP 5 or 5+. */
function __construct()
{
$this->dbLink = ”;
$this->sqlQuery = ”;
$this->dbResult = ”;
$this->dbRow = ”;
$this->mySalary = 0;
$this->myBonus = 0;

$this->dbLink = mysql_connect(‘localhost’, ‘root’, ”);
mysql_query(“SET character_set_results=utf8″, $this->dbLink);
mb_language(‘uni’);
mb_internal_encoding(‘UTF-8′);

mysql_select_db(‘test’, $this->dbLink);
mysql_query(“set names ‘utf8′”,$this->dbLink);
}

function displayTotalPayment()
{
mysql_query(“SET character_set_results=utf8″, $this->dbLink);
$this->sqlQuery = “SELECT * FROM bangla_number “;
$this->dbResult = mysql_query($this->sqlQuery, $this->dbLink);
$this->total = 0;
while($this->dbRow = mysql_fetch_object($this->dbResult))
{
echo ‘Employee ID # ‘ . $this->dbRow->id . ‘
‘;
echo ‘Salary: ‘ . $this->dbRow->salary . ‘
‘;
echo ‘Bonus: ‘ . $this->dbRow->bonus . ‘
‘;
$totalPayment = $this->convertToEnglishNumber($this->dbRow->salary) +
$this->convertToEnglishNumber($this->dbRow->bonus);
echo ‘Total Payment: ‘ . $this->convertToBanglaNumber($totalPayment) . ‘
‘;
echo ‘

‘;
}

}

function convertToEnglishNumber($unicodeNumber)
{

$englishNumber = mb_convert_encoding($unicodeNumber,”HTML-ENTITIES”,”UTF-8″);
$englishNumber = str_replace(‘০’, ’0′, $englishNumber);
$englishNumber = str_replace(‘১’, ’1′, $englishNumber);
$englishNumber = str_replace(‘২’, ’2′, $englishNumber);
$englishNumber = str_replace(‘৩’, ’3′, $englishNumber);
$englishNumber = str_replace(‘৪’, ’4′, $englishNumber);
$englishNumber = str_replace(‘৫’, ’5′, $englishNumber);
$englishNumber = str_replace(‘৬’, ’6′, $englishNumber);
$englishNumber = str_replace(‘৭’, ’7′, $englishNumber);
$englishNumber = str_replace(‘৮’, ’8′, $englishNumber);
$englishNumber = str_replace(‘৯’, ’9′, $englishNumber);
return $englishNumber;
}

function convertToBanglaNumber($englishNumber)
{
$englishNumber = (string) $englishNumber;
$banglaNumber = ”;
$indexLimit = strlen($englishNumber);
for($i=0; $i<$indexLimit; $i++)
{
switch($englishNumber[$i])
{
case "0":
$banglaNumber .= '০';
break;
case "1":
$banglaNumber .= '১';
break;
case "2":
$banglaNumber .= '২';
break;
case "3":
$banglaNumber .= '৩';
break;
case "4":
$banglaNumber .= '৪';
break;
case "5":
$banglaNumber .= '৫';
break;
case "6":
$banglaNumber .= '৬';
break;
case "7":
$banglaNumber .= '৭';
break;
case "8":
$banglaNumber .= '৮';
break;
case "9":
$banglaNumber .= '৯';
break;
default:
$banglaNumber .= $englishNumber[$i];
break;
}
}
return $banglaNumber;
}

}

?>
{/code}

If you are not using Bengali language, you need to configure the equivalent number values in convertToEnglishNumber and convertToBanglaNumber functions.

And here goes the page where I am executing the displayTotalPayment() function.

{code type=PHP}




include_once 'class.unicode.php';
$salaryObject = new UnicodeHandler();
$salaryObject->displayTotalPayment();

?>


{/code}

Now the final result looks like this:

Oh! One important thing. If you are coding for European number format, you need to do some modification to handle the comma (,) factor in the number in case required.

Thus, if you use this small tricky method, you can add, subtract,  multiply or divide any local numbers such as Russian, Chinese, Arabic, Spanish, German, Polish, Turkey, Hindi etc.

You can also download this small piece of code from here:
http://www.tanzilo.com/demo/code/unicode_number_add/unicode_number_add.zip

Thank you for reading.

posted under PHP
9 Comments to

“PHP & MySQL: Unicode number add, subtract etc for any language”

  1. On February 9th, 2009 at 6:24 am bill.rassel Says:

    Thnx brother, i m really benefited by ur tutorial, thnx a lot. so like this always i need to convert bangla (unicode) to english then whatever i do, like add, sum, sub etc.
    right?

  2. On February 9th, 2009 at 7:19 am admin Says:

    @ bill.rassel

    Yeah!
    You are right.

    And you are always welcome.

  3. On March 20th, 2009 at 8:02 am Tareq Says:

    Thanks for this. But, still converting is always need. We can’t do it without conversion :)

  4. On April 30th, 2009 at 6:51 pm jujulal Says:

    Yeah!
    your idea was good! i need really need this kind of conversion. I use it in nepali unicode language!
    :)

  5. On June 7th, 2009 at 11:27 am language learning software Says:

    You have a great blog here and it is Nice to read some well written posts that have some relevancy…keep up the good work ;)

  6. On July 21st, 2009 at 6:37 am Abdullah Al Mamun Says:

    If i wanna input Bengali numbers and then calculate regarding to their values, what are the steps i should flow???
    Thanks for your great tutorial.

  7. On July 21st, 2009 at 8:28 am admin Says:

    @ Abdullah Al Mamun

    you should try it yourself
    i am sure you can do it
    if you follow my tutorial closely, you can easily do that
    give a try!

  8. On December 15th, 2010 at 4:40 am Motahar Hossain Says:

    vaiya ami ekta PHP5 er bangla ebook- er address chai, jodi keu link dite paren ektu shohojogita korun. pls

  9. On December 15th, 2010 at 4:48 am admin Says:

    @ Motahar

    i think there is a PHP book by Suhrid Sorkar in the market. but not sure if there is any ebook version of this book.

Email will not be published

Website example

Your Comment: