How does Stack Overflow generate its SEO-friendly URLs?

后端 未结 21 2564
-上瘾入骨i
-上瘾入骨i 2020-11-22 04:27

What is a good complete regular expression or some other process that would take the title:

How do you change a title to be part of the URL like Stack

21条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-22 04:43

    Rewrite of Jeff's code to be more concise

        public static string RemapInternationalCharToAscii(char c)
        {
            var s = c.ToString().ToLowerInvariant();
    
            var mappings = new Dictionary
            {
                { "a", "àåáâäãåą" },
                { "c", "çćčĉ" },
                { "d", "đ" },
                { "e", "èéêëę" },
                { "g", "ğĝ" },
                { "h", "ĥ" },
                { "i", "ìíîïı" },
                { "j", "ĵ" },
                { "l", "ł" },
                { "n", "ñń" },
                { "o", "òóôõöøőð" },
                { "r", "ř" },
                { "s", "śşšŝ" },
                { "ss", "ß" },
                { "th", "Þ" },
                { "u", "ùúûüŭů" },
                { "y", "ýÿ" },
                { "z", "żźž" }
            };
    
            foreach(var mapping in mappings)
            {
                if (mapping.Value.Contains(s))
                    return mapping.Key;
            }
    
            return string.Empty;
        }
    

提交回复
热议问题