基本上,我的问题与this相同,但在Python(和GAE)中,而不是C#。
要求:
答案 0 :(得分:5)
def ToSeoFriendly(s, maxlen):
'''Join with dashes, eliminate punction, clip to maxlen, lowercase.
>>> ToSeoFriendly("The quick. brown4 fox jumped", 14)
'the-quick-brow'
'''
t = '-'.join(s.split()) # join words with dashes
u = ''.join([c for c in t if c.isalnum() or c=='-']) # remove punctation
return u[:maxlen].rstrip('-').lower() # clip to maxlen
if __name__ == '__main__':
import doctest
print doctest.testmod()
答案 1 :(得分:3)
您要搜索的字词为“slugify”。
答案 2 :(得分:3)
作为替代(可能是更多测试版本),我建议你使用Django的(最小修改)slugify代码:
import unicodedata
import re
def slugify(value):
"""
Normalizes string, converts to lowercase, removes non-alpha characters,
and converts spaces to hyphens.
"""
value = unicodedata.normalize('NFKD', value).encode('ascii', 'ignore').decode('ascii')
value = re.sub('[^\w\s-]', '', value).strip().lower()
return re.sub('[-\s]+', '-', value)
请参阅:https://github.com/django/django/blob/master/django/utils/text.py#L435