How does Stack Overflow generate its SEO-friendly URLs?

后端 未结 21 1883
-上瘾入骨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:53

    Here's my (slower, but fun to write) version of Jeff's code:

    public static string URLFriendly(string title)
    {
        char? prevRead = null,
            prevWritten = null;
    
        var seq = 
            from c in title
            let norm = RemapInternationalCharToAscii(char.ToLowerInvariant(c).ToString())[0]
            let keep = char.IsLetterOrDigit(norm)
            where prevRead.HasValue || keep
            let replaced = keep ? norm
                :  prevWritten != '-' ? '-'
                :  (char?)null
            where replaced != null
            let s = replaced + (prevRead == null ? ""
                : norm == '#' && "cf".Contains(prevRead.Value) ? "sharp"
                : norm == '+' ? "plus"
                : "")
            let _ = prevRead = norm
            from written in s
            let __ = prevWritten = written
            select written;
    
        const int maxlen = 80;  
        return string.Concat(seq.Take(maxlen)).TrimEnd('-');
    }
    
    public static string RemapInternationalCharToAscii(string text)
    {
        var seq = text.Normalize(NormalizationForm.FormD)
            .Where(c => CharUnicodeInfo.GetUnicodeCategory(c) != UnicodeCategory.NonSpacingMark);
    
        return string.Concat(seq).Normalize(NormalizationForm.FormC);
    }
    

    My test string:

    " I love C#, F#, C++, and... Crème brûlée!!! They see me codin'... they hatin'... tryin' to catch me codin' dirty... "

提交回复
热议问题