Java 和 SEO 友好的 URL:©reate ╨由特殊字符组成的字符串中的有效 http URL
我正在尝试从可能包含特殊字符、带重音符号的字母、类似中文的字符等的字符串中提取 SEO 友好的 URL。
SO 正在这样做,并且正在将这篇文章的标题翻译为“
java-and-seo-friendly-urls-reate--a-valid-http-url-from-a-string-composed-by-s
我正在尝试用 Java 执行此操作”。
我正在使用 这篇文章 解决方案和 URLEncoder .encode 将中文和其他符号翻译为有效的 URL 字符。
你曾经实现过类似的东西吗?有更好的办法吗?
I'm trying to extract SEO friendly URLs from strings that can contain special characters, letter with accents, Chinese like characters, etc.
SO is doing this and it's translating this post title in
java-and-seo-friendly-urls-reate--a-valid-http-url-from-a-string-composed-by-s
I'm trying to do this in Java.
I'm using this post solution with URLEncoder.encode to translate Chinese and other symbols into valid URL characters.
Have you ever implemented something like this? Is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能是解决问题的过于简单的方法,但您可以仅使用正则表达式来删除所有非标准字符。因此,将字符串转换为小写后,您可以将所有非小写字母字符替换为空字符,然后将所有空格替换为“-”字符。
This might be an oversimplistic approach to the problem, but you could just use regular expressions to remove all non standard characters. So after converting your string to lowercase, you can replace all non lowercase alphabetic characters with an empty character and then replace all spaces with the '-' character.
我不知道有什么标准方法,我一直在使用与您所指的类似的解决方案。不确定哪个更好,所以这里有:
}
I don't know of any standard way for this, I've been using a similair solution as what you are refering to. Not sure which one's better, so here you have it:
}
我想说 URLEncoder.encode 是可行的方法。所有非 URL 字符都会被映射,您肯定不想重新发明轮子(一次又一次)。
I would say URLEncoder.encode is the way to go. All non-URL chars are mapped, and you surely don't want to reinvent the wheel (again and again and again).