I am using the following JavaScript to convert all the English numbers showing on my website to Persian.
I realize that there are several ways to go about this and I have looked online and tried a few, however, this script does what I want it to do and it was really easy to implement except I want the phone number and date in my footer to be excluded from the conversion and remain English.
Not sure how I go about this in my PHP/HTML page.
On the second note, when my site is loading for a couple of seconds the numbers appear in English and quickly convert to Persian. Just wondering if there was a way that I could reduce this delay?
Here is my code:
<script language="JavaScript" type="text/javascript">
var replaceDigits = function() {
var map =
[
"&\#1776;","&\#1777;","&\#1778;","&\#1779;","&\#1780;",
"&\#1781;","&\#1782;","&\#1783;","&\#1784;","&\#1785;"
]
document.body.innerHTML =
document.body.innerHTML.replace(
/\d(?=[^<>]*(<|$))/g,
function($0) { return map[$0] }
);
}
</script>
<script type="text/javascript">
window.onload = replaceDigits
</script>
Here you are :)
function toFarsiNumber(n) {
const farsiDigits = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
return n
.toString()
.split('')
.map(x => farsiDigits[x])
.join('');
}
Another version!
function toFarsiNumber(n) {
const farsiDigits = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];
return n
.toString()
.replace(/\d/g, x => farsiDigits[x]);
}
Here is the PHP function :
function toPersianNum($number)
{
$number = str_replace("1","۱",$number);
$number = str_replace("2","۲",$number);
$number = str_replace("3","۳",$number);
$number = str_replace("4","۴",$number);
$number = str_replace("5","۵",$number);
$number = str_replace("6","۶",$number);
$number = str_replace("7","۷",$number);
$number = str_replace("8","۸",$number);
$number = str_replace("9","۹",$number);
$number = str_replace("0","۰",$number);
return $number;
}
Using :
$fa_num = toPersianNum(1234); // It returns ۱۲۳۴
$fa_date = toPersianNum('2016/05/10'); // It returns ۲۰۱۶/۰۵/۱۰
JavaScript
And here is the JavaScript function :
function toPersianNum( num, dontTrim ) {
var i = 0,
dontTrim = dontTrim || false,
num = dontTrim ? num.toString() : num.toString().trim(),
len = num.length,
res = '',
pos,
persianNumbers = typeof persianNumber == 'undefined' ?
['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'] :
persianNumbers;
for (; i < len; i++)
if (( pos = persianNumbers[num.charAt(i)] ))
res += pos;
else
res += num.charAt(i);
return res;
}
Using :
var fa_num = toPersianNum(1234) // It returns ۱۲۳۴
var fa_date = toPersianNum('2016/05/10'); // It returns ۲۰۱۶/۰۵/۱۰
Be Happy :)
UPDATE
Here are toEnglishNum()
as well.
PHP:
function toEnglishNum($number)
{
$number = str_replace("۱","1",$number);
$number = str_replace("۲","2",$number);
$number = str_replace("۳","3",$number);
$number = str_replace("۴","4",$number);
$number = str_replace("۵","5",$number);
$number = str_replace("۶","6",$number);
$number = str_replace("۷","7",$number);
$number = str_replace("۸","8",$number);
$number = str_replace("۹","9",$number);
$number = str_replace("۰","0",$number);
return $number;
}
Javascript:
function toEnglishNum( num, dontTrim ) {
var i = 0,
j = 0,
dontTrim = dontTrim || false,
num = dontTrim ? num.toString() : num.toString().trim(),
len = num.length,
res = '',
pos,
persianNumbers = typeof persianNumber == 'undefined' ?
[ '۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹' ] :
persianNumbers;
for ( ; i < len; i++ )
if ( ~( pos = persianNumbers.indexOf( num.charAt( i ) ) ) )
res += pos;
else
res += num.charAt( i );
return res;
};
Good Luck
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