16

while developing one of my sites, i noticed that if I enter arabic numbers (١٢٣), they are not interpreted as real number values. Then, I tested a few other sites only to find that they also don't accept arabic numbers.

The problem is, my client seems to require this functionality (accepting arabic numbers).. and I have no idea where to start. My platform is magento (php).

Nightfirecat
  • 11,432
  • 6
  • 35
  • 51
maha
  • 277
  • 1
  • 5
  • 15
  • I have tried to find you an answer, but unfortunately I can't find anything. All "solutions" are to change the arabic numbers into a normal number before you insert it into the textfield... If there are plugins for Magento, you could try a plugin. That's the best advice I can give. – Tim S. Nov 15 '11 at 07:46
  • thanks i appreciate your efforts anyways :) – maha Nov 15 '11 at 10:43

4 Answers4

6

AFAIK you need to install an arabic language pack with ./mage install - see http://www.magentocommerce.com/magento-connect/Ahmed+J.+Hadi/extension/353/magento-community-modules--arabic-saudi-arabia-language-pack

Yahia
  • 69,653
  • 9
  • 115
  • 144
  • I already have installed an arabic language pack.. but that didn't do the trick :( – maha Nov 15 '11 at 10:45
  • @maha can't try it out right now... but then the only option is to path magento (for example the client-side JS validation needs to "translate the numbers") – Yahia Nov 15 '11 at 10:50
4

To allow PHP to accept the Arabic numbers or Persian numbers (Farsi) (١٢٣٤٥), I developed this easy function:

<?php

/*
/////////////////////
This function has been created by Abdulfattah alhazmi
Roles:
To convert Arabic/Farsi Numbers (٠‎ - ١‎ - ٢‎ - ٣‎ - ٤‎ - ٥‎ - ٦‎ - ٧‎ - ٨‎ - ٩‎) 
TO the corrosponding English numbers (0-1-2-3-4-5-6-7-8-9)
http://hazmi.co.cc
/////////////////////
*/

function convertArabicNumbers($arabic) {
    $trans = array(
        "&#1632;" => "0",
        "&#1633;" => "1",
        "&#1634;" => "2",
        "&#1635;" => "3",
        "&#1636;" => "4",
        "&#1637;" => "5",
        "&#1638;" => "6",
        "&#1639;" => "7",
        "&#1640;" => "8",
        "&#1641;" => "9",
    );
    return strtr($arabic, $trans);
}
?>

Note: TO GET THE CORRECT RESULT from text field in your form, you have to use the htmlspecialchars_decode() function. For example:

$mytext = htmlspecialchars_decode($_POST['mytext']));
$mytext = convertArabicNumbers($mytext);

To keep your code safe, add strip_tags(). For example:

$mytext = strip_tags(htmlspecialchars_decode($_POST['mytext']));
$mytext = convertArabicNumbers($mytext);

Please don't hesitate to ask me if you have any further question about this function.

1

Strangely Abdulfattah Alhazmi function didn't worked with me, after lot of try & error, the below worked with me.

function convertArabicNumbers($arabic) {
    $trans = array(
        "٠" => "0",
        "١" => "1",
        "٢" => "2",
        "٣" => "3",
        "٤" => "4",
        "٥" => "5",
        "٦" => "6",
        "٧" => "7",
        "٨" => "8",
        "٩" => "9",
    );
    return strtr($arabic, $trans);
}
aliawadh980
  • 468
  • 6
  • 5
1

Actually, Arabic numbers are not all the same as Persian. for example, ۴ and ۵ (4 and 5 in Persian) are different from ٤ and ٥ (4 and 5 in Arabic). I think ۶ (6) is also different. so one needs to use Unicode for actual "Persian" numbers (listed here) if they wanted to get correct Persian numbers.