如何翻译“Lorem 3 ipsum dolor sat amet”进入 SEO 友好的“Lorem-3-ipsum-dolor-sit-amet”在Java中?
在我的博客应用程序中,用户可以输入任何文本作为其条目的标题,然后我根据该文本生成一个 URL。
我验证他们的标题以确保它仅包含字母和数字。
如果他们输入类似
Lorem 3 ipsum dolor sit amet
我如何生成此文本的更 SEO 友好版本的内容:
Lorem-3-ipsum-dolor-sit-amet
In my blog app, a user can enter any text as a title for their entry and then I generate a URL based on the text.
I validate their title to make sure it only contains letters and numbers.
If they enter something like
Lorem 3 ipsum dolor sit amet
how could I generate the more SEO friendly version of this text:
Lorem-3-ipsum-dolor-sit-amet
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
因为它似乎不允许我发表评论。我会这样做:
使用下划线字符代替,因为它是一个空格指示符。自从我完成 java 以来已经有一段时间了,但我知道 .Net 中有一个函数可以清理文件名,这样它对文件系统来说是安全的。我有很多相同的一般规则适用于 URL,因此如果您可以在 API 中找到一个,那么值得一看。
Since it won't seem to allow me to comment. I would do:
Using the Underscore character instead because it is a space indicator. Its been a while since I've done java but I know there is a function in .Net that will cleanup a file name so its safe for the file system. I lot of the same general rules applies to a URL so if you can find one in the API it be worth taking a look.
实际上,这并不像用连字符替换空格那么简单。您通常还希望将其全部小写并标准化/替换变音符号,例如 á、ö、è 等无效 URL 字符。唯一有效的字符在此维基百科页面。
这样的函数如下所示:
它基本上执行以下操作:
另请参阅:
It's in practice really not as simple as replacing spaces by hypens. You would often also like to make it all lowercase and normalize/replace diacritics, like á, ö, è and so on which are invalid URL characters. The only valid characters are listed as "Unreserved characters" in the 2nd table of this Wikipedia page.
Here's how such a function can look like:
It does basically the following:
See also: