Skip to content

Instantly share code, notes, and snippets.

@baghayi-gist
Created November 3, 2012 22:15
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save baghayi-gist/4009084 to your computer and use it in GitHub Desktop.
Save baghayi-gist/4009084 to your computer and use it in GitHub Desktop.
PHP: Converting Persian Numbers To English One (function).
function convertPersianNumbersToEnglish($number)
{
$persian = array('۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹');
$num = range(0, 9);
return (int)str_replace($persian, $num, $number);
}
@AmirFayaz
Copy link

function convertPersianNumbersToEnglish($input)
{
    $persian = ['۰', '۱', '۲', '۳', '۴', '٤', '۵', '٥', '٦', '۶', '۷', '۸', '۹'];
    $english = [ 0 ,  1 ,  2 ,  3 ,  4 ,  4 ,  5 ,  5 ,  6 ,  6 ,  7 ,  8 ,  9 ];
    return str_replace($persian, $english, $input);
}

It converts Persian/Arabic numbers to English

@saman
Copy link

saman commented Apr 28, 2020

function convertEnglishNumbersToPersian($input)
{
    $persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
    $english = [ 0 ,  1 ,  2 ,  3 ,  4 ,  5 ,  6 ,  7 ,  8 ,  9 ];
    return str_replace($english, $persian, $input);
}

@TheMMB
Copy link

TheMMB commented Aug 23, 2020

private function conToEn($string) {
$range = range(0, 9);
// Persian
$persianDecimal = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
// Arabic
$arabicDecimal = ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];
// number Arabic
$arabic = ['٠', '١', '٢', '٣', '٤', '٥', '٦', '٧', '٨', '٩'];
// number Persian
$persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];

$string =  str_replace($persianDecimal, $range, $string);
$string =  str_replace($arabicDecimal, $range, $string);
$string =  str_replace($arabic, $range, $string);
return str_replace($persian, $range, $string);

}

@javadghane
Copy link

 fun replacePersianAndArabic(txt: String): String {
            return txt.replace("۰", "0")
                .replace("۱", "1")
                .replace("۲", "2")
                .replace("۳", "3")
                .replace("۴", "4")
                .replace("۵", "5")
                .replace("۶", "6")
                .replace("۷", "7")
                .replace("۸", "8")
                .replace("۹", "9")
                .replace("٩", "9")
                .replace("٨", "8")
                .replace("٧", "7")
                .replace("٦", "6")
                .replace("٥", "5")
                .replace("٤", "4")
                .replace("٣", "3")
                .replace("٢", "2")
                .replace("١", "1")
                .replace("٠", "0")

        }

same work in kotlin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment