Skip to content

Instantly share code, notes, and snippets.

@unicornist
Last active February 21, 2019 18:35
Show Gist options
  • Save unicornist/9226220 to your computer and use it in GitHub Desktop.
Save unicornist/9226220 to your computer and use it in GitHub Desktop.
Persian to English (and vice versa) Digit convert in JavaScript and PHP
// this version is much more performant than previous one (see gist revisions)
var FarsiDigits = {
toEn: function(input) { return (input+'').replace(FarsiDigits._regexFa, FarsiDigits.charToEn); },
toFa: function(input) { return (input+'').replace(FarsiDigits._regexEn, FarsiDigits.charToFa); },
charToFa: function(char){ return String.fromCharCode(char.charCodeAt(0)+0x6c0); },
charToEn: function(char){ return String.fromCharCode(char.charCodeAt(0)-0x6c0); },
_regexFa: /[\u06f0-\u06f9]/g,
_regexEn: /[0-9]/g
};
<?
function farsi_digits($str) {
$farsi_array = array("۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹");
$english_array = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
return str_replace($english_array, $farsi_array, $str);
}
function unfarsi_digits($str) {
$farsi_array = array("۰", "۱", "۲", "۳", "۴", "۵", "۶", "۷", "۸", "۹");
$english_array = array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9");
return str_replace($farsi_array, $english_array, $str);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment