How does Stack Overflow generate its SEO-friendly URLs?

后端 未结 21 1854
-上瘾入骨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 05:03

    T-SQL implementation, adapted from dbo.UrlEncode:

    CREATE FUNCTION dbo.Slug(@string varchar(1024))
    RETURNS varchar(3072)
    AS
    BEGIN
        DECLARE @count int, @c char(1), @i int, @slug varchar(3072)
    
        SET @string = replace(lower(ltrim(rtrim(@string))),' ','-')
    
        SET @count = Len(@string)
        SET @i = 1
        SET @slug = ''
    
        WHILE (@i <= @count)
        BEGIN
            SET @c = substring(@string, @i, 1)
    
            IF @c LIKE '[a-z0-9--]'
                SET @slug = @slug + @c
    
            SET @i = @i +1
        END
    
        RETURN @slug
    END
    

提交回复
热议问题