如何翻译“Lorem 3 ipsum dolor sat amet”进入 SEO 友好的“Lorem-3-ipsum-dolor-sit-amet”在Java中?

发布于 2024-09-17 14:39:23 字数 260 浏览 11 评论 0原文

在我的博客应用程序中,用户可以输入任何文本作为其条目的标题,然后我根据该文本生成一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

荆棘i 2024-09-24 14:40:20

因为它似乎不允许我发表评论。我会这样做:

String s = "Lorem 3 ipsum dolor sit amet"
s = s.replaceAll(" ","_");

使用下划线字符代替,因为它是一个空格指示符。自从我完成 java 以来已经有一段时间了,但我知道 .Net 中有一个函数可以清理文件名,这样它对文件系统来说是安全的。我有很多相同的一般规则适用于 URL,因此如果您可以在 API 中找到一个,那么值得一看。

Since it won't seem to allow me to comment. I would do:

String s = "Lorem 3 ipsum dolor sit amet"
s = s.replaceAll(" ","_");

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.

疯到世界奔溃 2024-09-24 14:40:11
String s = "Lorem 3 ipsum dolor sit amet"
s = s.replaceAll(" ","-");
String s = "Lorem 3 ipsum dolor sit amet"
s = s.replaceAll(" ","-");
A君 2024-09-24 14:40:01

实际上,这并不像用连字符替换空格那么简单。您通常还希望将其全部小写并标准化/替换变音符号,例如 á、ö、è 等无效 URL 字符。唯一有效的字符在此维基百科页面。

这样的函数如下所示:

public static String prettyURL(String string) {
    return Normalizer.normalize(string.toLowerCase(), Form.NFD)
        .replaceAll("\\p{InCombiningDiacriticalMarks}+", "")
        .replaceAll("[^\\p{Alnum}]+", "-");
}

它基本上执行以下操作:

另请参阅:

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:

public static String prettyURL(String string) {
    return Normalizer.normalize(string.toLowerCase(), Form.NFD)
        .replaceAll("\\p{InCombiningDiacriticalMarks}+", "")
        .replaceAll("[^\\p{Alnum}]+", "-");
}

It does basically the following:

  • lowercase the string
  • remove combining diacritical marks (after the Normalizer has "extracted" them from the actual chars)
  • replace non-alphanumeric characters by hyphens

See also:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文