"converting arabic numerals to arabic/persian numbers in html file" Code Answer


you can use a regular expression to find the parts of the html that are between '>' and '<' characters, and operate on those. this will prevent the code from processing the tag names and attributes (style, etc).

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

this isn't perfect, since it doesn't check at all for html character specifiers outside of the tags, such as the construct &#<digits>; (&#1777; for ۱, etc)to specify a character by unicode value, and will replace the digits in these. it also won't process any extra text before the first tag or after the last tag.


calling: toarabicnumshtml("<html><body><h1>i was born in 1988</h1></body></html>")
result: "<html><body><h1>i was born in ۱۹۸۸</h1></body></html>"

use whatever code you prefer in toarabicnums to do the actual transformation, or generalize it by passing in a transformation function.

By discipulus on September 24 2022

Answers related to “converting arabic numerals to arabic/persian numbers in html file”

Only authorized users can answer the Search term. Please sign in first, or register a free account.