问题
I want to sanitize blog titles with unicode characters in url. I need to replace invalid characters and spaces with "-" for better seo rewriting like this.
http://example.com/это-моя-хорошая
Can anyone have any idea how to do it?
回答1:
You can use this algorithm for an SEO-friendly Unicode URL:
- Convert the text to Unicode Normalization Form C, i.e. precomposed characters.
- Use a regular expression with Unicode character classes to replace each non-letter non-digit character with a space.
- Remove leading, trailing and double spaces.
- Shorten.
- Replace spaces with hyphens.
来源:https://stackoverflow.com/questions/9511254/how-to-create-unicode-slug-for-unicode-title