How can I convert Persian/Arabic digits from the string to English digits?

Viewed 687

I have a website in Persian. The keyboard of my website's users is in Persian (or Arabic) usually. So their password will not get matched sometimes.

I have a function for their username (cell-phone number) which converts Persian/Arabic digits to English:

function convert_digits_to_en($entry){
    $fmt = numfmt_create('fa', NumberFormatter::DECIMAL);
    return numfmt_parse($fmt, $entry);
}

It will work if the entry contains all digits.

i.e. ۰۹۱۲۳۵۶۵۴۹۸ will be converted to 09123565498 as well.


The problem is when the entry contains both characters and digits (like a password). i.e. test۰۹۱۲. I need to convert it to test0912. My current function returns an empty string for that entry. Any idea how can I fix it?

4 Answers

I searched a lot for validating Persian phone numbers with Persian characters like ۱۲۳۴ using regex in Laravel but found no suitable answer so instead of validating Persian number I decided to change Persian numbers to English and validate in myself, it helped me a lot, hope this helps:

if (is_numeric($mobile) && strlen($mobile) == 11) {
      // if number in english
}else{
    $mobile = str_split($mobile , 2);
    if (count($mobile) != 11) {
        return redirect()->back()->withErrors('فرمت شماره موبایل باید عدد و ۱۱ رقم باشد');
    }
    foreach ($mobile as $key => $number) {
        if ($number == '۰') {
            $mobile[$key] = 0;
        }elseif ($number == '۱') {
            $mobile[$key] = 1;
        }elseif ($number == '۲') {
            $mobile[$key] = 2;
        }elseif ($number == '۳') {
            $mobile[$key] = 3;
        }elseif ($number == '۴') {
            $mobile[$key] = 4;
        }elseif ($number == '۵') {
            $mobile[$key] = 5;
        }elseif ($number == '۶') {
            $mobile[$key] = 6;
        }elseif ($number == '۷') {
            $mobile[$key] = 7;
        }elseif ($number == '۸') {
            $mobile[$key] = 8;
        }elseif ($number == '۹') {
            $mobile[$key] = 9;
        }
    }
    $mobile = implode($mobile);
    if(is_numeric($mobile) == false){
        return redirect()->back()->withErrors('فرمت شماره موبایل باید عدد و ۱۱ رقم باشد');
    }
}

A non-regex solution would be this:

function convert_digits_to_en($input)
{
  $arabic = ['۰' => 0, '۱' => 1];

  return strtr($input, $arabic);
}

echo convert_digits_to_en('test۱۰۰'); // test100

Note: You'd need to add in the numbers to that array, my Persian / Arabic is not that good.

Live Example

Repl

Reading Material

strtr

you can use your string as array and iterate them like:

function convert_digits_to_en($entry){
  $result = '';
  $fmt = numfmt_create('fa', NumberFormatter::DECIMAL);
  foreach((array)$entry as $value){
    $transform_digit = numfmt_parse($fmt, $value);;
    $result = empty($transform_digit) ? $value : $transform_digit;
  }
  return $result;
}

As a short and fast solution, I suggest try this:

function fa2en($str) {
    return strtr($str, array('۰'=>'0', '۱'=>'1', '۲'=>'2', '۳'=>'3', '۴'=>'4', '۵'=>'5', '۶'=>'6', '۷'=>'7', '۸'=>'8', '۹'=>'9'));
}
Related