用于清理用户输入标题以使其显示在URL中的Java库? [英] Java library for cleaning up user-entered title to make it show up in a URL?

查看:128
本文介绍了用于清理用户输入标题以使其显示在URL中的Java库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个Web应用程序。我希望有一个SEO友好的链接,如下所示:

I am doing a web application. I would like to have a SEO-friendly link such as the following:

http://somesite.org/user-entered-title

以上用户输入的标题是从用户创建的记录中提取的,这些记录中有一个字段被调用标题。

The above user-entered-title is extracted from user-created records that have a field called title.

我想知道是否有任何Java库用于清理这些用户输入的文本(例如删除空格),然后再将其显示在URL中。

I am wondering whether there is any Java library for cleaning up such user-entered text (remove spaces, for example) before displaying it in a URL.

从用户输入的stackoverflow很好清理后,我的目标文本就像stackoverflow-is-great。

My target text is something such as "stackoverflow-is-great" after cleanup from user-entered "stackoverflow is great".

我能够编写代码来用短划线替换字符串中的空格,但不确定其他规则/想法/最佳实践是什么使文本成为网址的一部分。

I am able to write code to replace spaces in a string with dashes, but not sure what are other rules/ideas/best practices out there for making text part of a url.

请注意,用户输入的标题可能使用不同的语言,而不仅仅是英语。

Please note that user-entered-title may be in different languages, not just English.

感谢您提供任何输入和指示!

Thanks for any input and pointers!

问候。

推荐答案

你想要的是什么某种 SLUGifying 前缀为URL,因此它对搜索引擎优化非常友好。

What you want is some kind of "SLUGifying" the prhase into a URL, so it is SEO-friendly.

一旦我遇到这个问题,我来到了使用 maddemcode.com 中提供的解决方案。下面你会找到它改编的代码。

Once I had that problem, I came to use a solution provided in maddemcode.com. Below you'll find its adapted code.

诀窍是正确使用 Normalize JDK类额外的清理。用法很简单:

The trick is to properly use the Normalize JDK class with some little additional cleanup. The usage is simple:

// casingchange-aeiouaeiou-takesexcess-spaces
System.out.println(slugify("CaSiNgChAnGe áéíóúâêîôû   takesexcess    spaces  "));
// these-are-good-special-characters-sic
System.out.println(slugify("These are good Special Characters šíč"));
// some-exceptions-123-aeiou
System.out.println(slugify(" some exceptions ¥123  ã~e~iõ~u!@#$%¨&*() "));
// gonna-accomplish-yadda
System.out.println(slugify("gonna accomplish, yadda, 완수하다, 소양양)이 있는 "));

功能代码:

public static String slugify(String input) {
    return Normalizer.normalize(input, Normalizer.Form.NFD)
            .replaceAll("[^\\p{ASCII}]", "")
            .replaceAll("[^ \\w]", "").trim()
            .replaceAll("\\s+", "-").toLowerCase(Locale.ENGLISH);
}

在源页面中( http://maddemcode.com/java/seo-friendly-urls-using-slugify-in-java/ )你可以看看它来自哪里。但是,上面的小片段的工作原理相同。

In the source page (http://maddemcode.com/java/seo-friendly-urls-using-slugify-in-java/) you can take a look at where this comes from. The small snippet above, though, works the same.

正如您所看到的,有一些特殊的字符未被转换。据我所知,翻译它们的每个人都使用某种地图,比如Djago的urlify(查看示例这里映射)。你需要它们,我相信你最好的选择是制作一个。

As you can see, there are some exceptional chars that aren't converted. To my knowledge, everyone that translates them, uses some kind of map, like Djago's urlify (see example map here). You need them, I believe your best bet is making one.

这篇关于用于清理用户输入标题以使其显示在URL中的Java库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆