将英语数字转换为波斯语

2022-08-31 00:00:24

我正在使用以下JavaScript将我网站上显示的所有英文数字转换为波斯语。

我意识到有几种方法可以做到这一点,我已经在网上查看并尝试了一些,但是,这个脚本做了我想要它做的事情,它真的很容易实现,除了我希望我的页脚中的电话号码和日期被排除在转换之外并保持英语。

不知道我是如何在我的PHP / HTML页面中做到这一点的。

第二点,当我的网站加载几秒钟时,数字以英文显示,并迅速转换为波斯语。只是想知道是否有办法减少这种延迟?

这是我的代码:

<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>

答案 1

你在这里:)

function toFarsiNumber(n) {
    const farsiDigits = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];

    return n
      .toString()
      .split('')
      .map(x => farsiDigits[x])
      .join('');
  }

另一个版本!

function toFarsiNumber(n) {
    const farsiDigits = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];

    return n
        .toString()
        .replace(/\d/g, x => farsiDigits[x]);
}

答案 2

下面是 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;
};

祝你好运