Skip to content

Instantly share code, notes, and snippets.

@RyadPasha
Last active July 20, 2019 10:53
Show Gist options
  • Save RyadPasha/f3c21af8f3402769225cb807c54e64a6 to your computer and use it in GitHub Desktop.
Save RyadPasha/f3c21af8f3402769225cb807c54e64a6 to your computer and use it in GitHub Desktop.
Convert Arabic/Persian numbers to English numbers in PHP
<?php
function conv2EnNum($str) {
$str = str_replace(['٠','١','٢','٣','٤','٥','٦','٧','٨','٩'], range(0, 9), $str); // Convert Arabic numbers
$str = str_replace(['۰','۱','۲','۳','۴','۵','۶','۷','۸','۹'], range(0, 9), $str); // Convert Persian numbers
return preg_replace('/[^0-9\.,]/', '', $str);
}
echo conv2EnNum('٠١٢٣٤٥٦٧٨٩'); // -> 01213456789
echo conv2EnNum('١٣.٥'); // -> 13.5
echo conv2EnNum('۴۷۶'); // -> 476
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment