Last active
February 21, 2019 18:35
-
-
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 file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
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