DucDigital for ( $girl = 1; $girl < $required; $girl++ ) { echo “I love DucDigital”; }

30Nov/090

Convert latin accent to non-latin accent (Normalization Form)

I'm sure some of you have been tried to remove the special accent sign like "ắ ế í u ư đ" and replace it to a new string without accent for tagging, sluging, permalink purpose in your webpage...

this function use for asp.net / C# to replace everything to Normalization Form...

Tiếng việt:

Chắc hẳn một số bạn muốn thay các chữ có dấu tiếng vịêt thành chữ không dấu để lưu vào cơ sở dữ liệu, làm permalink hoặc một số chức năng khác, đây là function để thay thế toàn bộ chữ có dấu thành không dấu trong tiếng việt.

Chúc các bạn may mắn /  Good luck

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
        public static string LatinToAscii(string InString)
        {
            string newString = string.Empty, charString;
            char ch;
            int charsCopied;
 
            for (int i = 0; i < InString.Length; i++)
            {
                charString = InString.Substring(i, 1);
                charString = charString.Normalize(NormalizationForm.FormKD);
                // If the character doesn't decompose, leave it as-is
 
                if (charString.Length == 1)
                    newString += charString;
                else
                {
                    charsCopied = 0;
                    for (int j = 0; j < charString.Length; j++)
                    {
                        ch = charString[j];
                        // If the char is 7-bit ASCII, add
 
                        if (ch < 128)
                        {
                            newString += ch;
                            charsCopied++;
                        }
                    }
                    /* If we've decomposed non-ASCII, give it back
                     * in its entirety, since we only mean to decompose
                     * Latin chars.
                    */
                    if (charsCopied == 0)
                        newString += InString.Substring(i, 1);
                }
            }
            return newString.Replace('đ', 'd');
        }

Modify from source: http://www.codeproject.com/KB/cs/UnicodeNormalization.aspx

  • Share/Bookmark
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.