下面是 PHP 函数:
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;
}
用:
$fa_num = toPersianNum(1234); // It returns ۱۲۳۴
$fa_date = toPersianNum('2016/05/10'); // It returns ۲۰۱۶/۰۵/۱۰
JavaScript
这是JavaScript函数:
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;
}
用:
var fa_num = toPersianNum(1234) // It returns ۱۲۳۴
var fa_date = toPersianNum('2016/05/10'); // It returns ۲۰۱۶/۰۵/۱۰
快乐:)
更新
这里也是。toEnglishNum()
菲律宾比索:
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;
};
祝你好运