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
  • 5,689
  • 5
  • 42
  • 92
  • 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
  • 1
    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

3 Answers3

3

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 peisan 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('فرمت شماره موبایل باید عدد و ۱۱ رقم باشد');
            }
        }
ehsan asarzadeh
  • 327
  • 1
  • 3
  • 11
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
  • 13,644
  • 4
  • 41
  • 60
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
  • 358
  • 2
  • 18