4

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?

Martin AJ
  • 6,261
  • 8
  • 53
  • 111
  • I just had a déjà vu, I've definitely seen this / similar question asked today and it was suggested to use client side and not PHP. **Edit:** Turns out it was something else, similar but not same: https://stackoverflow.com/questions/53264154/how-to-replace-entire-numbers-in-a-website-to-persian-numbers-via-php/53322340#53322340 – Script47 Nov 15 '18 at 15:35
  • regex is probably best suited for this. – noid Nov 15 '18 at 15:37
  • 3
    Possible duplicate of [convert Persian/Arabic numbers to English numbers](https://stackoverflow.com/questions/11766726/convert-persian-arabic-numbers-to-english-numbers) – Erik Kalkoken Nov 15 '18 at 15:44

4 Answers4

0

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

Script47
  • 14,230
  • 4
  • 45
  • 66
0

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;
}
episch
  • 388
  • 2
  • 19
0

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 it 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('فرمت شماره موبایل باید عدد و ۱۱ رقم باشد');
    }
}
ehsan asarzadeh
  • 157
  • 1
  • 5
  • 12
0

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'));
}
Saghachi
  • 851
  • 11
  • 19
  • 1
    You should have edited [Script47's answer](https://stackoverflow.com/a/53323027/3789665). – greybeard Sep 20 '22 at 10:12
  • Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – CherryDT Sep 20 '22 at 10:13
  • @greybeard Suggested edit queue is full. – Saghachi Sep 20 '22 at 14:34