在html文件中将阿拉伯数字转换为阿拉伯语/波斯语数字

时间:2013-02-14 05:31:15

标签: javascript converter arabic digits

我正在尝试将纯文本阿拉伯数字转换为东部阿拉伯数字。所以基本上采用 1 2 3 ... 并将它们转换为 1 2 3 ... 。该函数会转换所有数字,包括标记中包含的任何数字,即H1

 private void LoadHtmlFile(object sender, EventArgs e)
        {
            var htmlfile = "<html><body><h1>i was born in 1988</h1></body></html>".ToArabicNumber(); ;
            webBrowser1.DocumentText=htmlfile;
        }


    }
    public static class StringHelper
    {
        public static string ToArabicNumber(this string str)
        {
            if (string.IsNullOrEmpty(str)) return "";
            char[] chars;
            chars = str.ToCharArray();
            for (int i = 0; i < str.Length; i++)
            {
                if (str[i] >= '0' && str[i] <= '9')
                {
                    chars[i] += (char)1728;
                }
            }
            return new string(chars);
        }
    }

我也尝试过只定位InnerText中的数字,但它也没有用。下面的代码也会更改标签号。

private void LoadHtmlFile(object sender, EventArgs e)
        {
            var htmlfile = "<html><body><h1>i was born in 1988</h1></body></html>" ;
            webBrowser1.DocumentText=htmlfile;
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            webBrowser1.Document.Body.InnerText = webBrowser1.Document.Body.InnerText.ToArabicNumber();
        }

有什么建议吗?

4 个答案:

答案 0 :(得分:2)

您可以使用正则表达式查找“​​&gt;”之间的HTML部分和'&lt;'字符,并对这些字符进行操作。这将阻止代码处理标记名称和属性(样式等)。

// Convert all English digits in a string to Arabic digit equivalents
public static string ToArabicNums(string src)
{
    const string digits = "۰۱۲۳۴۵۶۷۸۹";
    return string.Join("", 
        src.Select(c => c >= '0' && c <= '9' ? digits[((int)c - (int)'0')] : c)
    );
}

// Convert all English digits in the text segments of an HTML 
// document to Arabic digit equivalents
public static string ToArabicNumsHtml(string src)
{
    string res = src;

    Regex re = new Regex(@">(.*?)<");

    // get Regex matches 
    MatchCollection matches = re.Matches(res);

    // process in reverse in case transformation function returns 
    // a string of a different length
    for (int i = matches.Count - 1; i >= 0; --i)
    {
        Match nxt = matches[i];
        if (nxt.Groups.Count == 2 && nxt.Groups[1].Length > 0)
        {
            Group g = nxt.Groups[1];
            res = res.Substring(0, g.Index) + ToArabicNums(g.Value) +
                res.Substring(g.Index + g.Length);
    }

    return res;
}

这并不完美,因为它根本不检查标记之外的HTML字符说明符,例如构造&#<digits>;&#1777;表示1等)来指定字符按Unicode值,并将替换这些中的数字。它也不会在第一个标记之前或最后一个标记之后处理任何额外的文本。

样品:

Calling: ToArabicNumsHtml("<html><body><h1>I was born in 1988</h1></body></html>")
Result: "<html><body><h1>I was born in ۱۹۸۸</h1></body></html>"

ToArabicNums中使用您喜欢的任何代码进行实际转换,或通过传入转换函数对其进行概括。

答案 1 :(得分:0)

使用正则表达式。这是我自己使用的JavaScript代码:

function toIndic(n) {
    var ns = ['۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹'];

    return n.toString().replace(/\d/g, function (m) { 
        return ns[m];
    });
}

为了确保您只转换数字,您可以使用更好的正则表达式:\ b [0-9] + \ b

答案 2 :(得分:0)

此功能可将英语转换为波斯语,阿拉伯语和ordu

function convertDigitIn(enDigit){ // PERSIAN, ARABIC, URDO
    var newValue="";
    for (var i=0;i<enDigit.length;i++)
    {
        var ch=enDigit.charCodeAt(i);
        if (ch>=48 && ch<=57
        {
            // european digit range
            var newChar=ch+1584;
            newValue=newValue+String.fromCharCode(newChar);
        }
        else
            newValue=newValue+String.fromCharCode(ch);
    }
    return newValue;
}

答案 3 :(得分:0)

只需在文档的末尾添加它,它就可以正常工作: - )

params['val']
相关问题