Skip to content

Instantly share code, notes, and snippets.

@baghayi-gist
Created November 3, 2012 22:15
Show Gist options
  • 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);
}
@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