How can I convert Persian/Arabic numbers to English numbers with a simple function ?
Persian/Arabic numbers:
۰ // -> 0 ۱ // -> 1 ۲ // -> 2 ۳ // -> 3 ۴ // -> 4 ۵ // -> 5 ۶ // -> 6 ۷ // -> 7 ۸ // -> 8 ۹ // -> 9
numbers over the unicode :
$num0="۰"; $num1="۱"; $num2="۲"; $num3="۳"; $num4="۴"; $num5="۵"; $num6="۶"; $num7="۷"; $num8="۸"; $num9="۹";
Here's a short function:
function convert($string) { $persian = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹']; $arabic = ['٩', '٨', '٧', '٦', '٥', '٤', '٣', '٢', '١','٠']; $num = range(0, 9); $convertedPersianNums = str_replace($persian, $num, $string); $englishNumbersOnly = str_replace($arabic, $num, $convertedPersianNums); return $englishNumbersOnly; }
You can use the unicode instead of the characters in $persian
(I think).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With