Strona główna > Java > java seo friendly url (slug)

java seo friendly url (slug)

Tworząc aplikację internetową, często w adresach pojawia się jakiś tekst, np tytuł wprowadzony przez użytkownika podczas dodawania artykuły, chociażby blogi na wordpressie.
Jednak adres w postaci http://www.ryznar.eu/artykul/śmieszny-artykuł podobno jest gorzej odbierany przez google, dwa w niektórych przeglądarkach jest on niezbyt czytelny bo przeglądarka nie potrafi przetworzyć np.: IE.
Dlatego tekst wprowadzony przez użytkownika należy nieco zmodyfikować. Z racji ze potrzebuję takiej funkcji napisałem sobie odpowiednią klasę:

import java.text.Normalizer;
import java.text.Normalizer.Form;

public class Slug {

    public static String parse(String input) {
        if (isEmptyOrNull(input)) {
            return "";
        }
        String out = normalize(input);
        out = removeDuplicateWhiteSpaces(out);
        out = out.toLowerCase();
        out = out.replace(" ", "-");

        return out;
    }

    private static String normalize(String input) {
        String result = Normalizer.normalize(input, Form.NFD).replaceAll("[^\\p{ASCII}]", ""); // 1
        result = result.replaceAll("[^a-zA-Z0-9\\s]", " "); // 2

        return result;
    }

    private static String removeDuplicateWhiteSpaces(String input) {
        return input.replaceAll("\\s+", " ");
    }

    private static boolean isEmptyOrNull(String input) {
        return (input == null || input.trim().length() == 0);
    }
}

1) tekst jest normalizowany, tzn znaki diakrytyczne są zmieniane na znaki ASCII
2) tutaj zmieniamy wszystkie znaki niealfanumeryczne na spacje

Możemy przetestować klasę:

        String inputText = "123-test       nąśŹA       zima 🙂 !! #@ zła";
        System.out.println(inputText);
        System.out.println(Slug.parse(inputText));

wynik: 123-test-nasza-zima-za – zjadło literkę „l”
okazuje się że na l może być zamieniony a nie ł
Niestety w sieci nie widzę żadnej wskazówki jak ładnie rozwiązać ten problem, więc na razie pozostaje mi tylko zmodyfikować ciało funkcji normalize:

        String result = input.replace("ł", "l").replace("Ł", "L");

        result = Normalizer.normalize(result, Form.NFD).replaceAll("[^\\p{ASCII}]", ""); // 1
        result = result.replaceAll("[^a-zA-Z0-9\\s]", " "); // 2

jeśli ktoś wie jak to zrobić normalnie a nie poprzez takie kombinacje, będę wdzięczny za komentarz

Reklamy
Kategorie:Java Tagi: , , ,
  1. 20 grudnia 2012 o 16:52

    Howdy I am so glad I found your blog, I really found you by accident, while I was looking on Askjeeve for something else,
    Anyways I am here now and would just like to say thanks a lot for a
    tremendous post and a all round enjoyable blog (I also love the theme/design), I don’t have time to browse it all at the moment but I have book-marked it and also added your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the fantastic job.

  2. 6 stycznia 2013 o 7:28

    This is a topic that is close to my heart… Best wishes! Exactly where are
    your contact details though?

  3. 14 stycznia 2013 o 21:49

    Hey there, You’ve done a fantastic job. I’ll definitely digg it
    and personally suggest to my friends. I’m sure they’ll be benefited from this site.

  4. 29 stycznia 2013 o 10:31

    Marvelous, what a blog it is! This website presents helpful
    information to us, keep it up.

  5. 8 kwietnia 2013 o 18:44

    I have fun with, lead to I discovered just what I was taking a look for.
    You’ve ended my 4 day long hunt! God Bless you man. Have a nice day. Bye

  6. seo
    10 kwietnia 2013 o 8:22

    What i do not understood is actually how you are now not actually much
    more well-liked than you may be right now. You are so intelligent.
    You realize therefore significantly on the subject
    of this topic, produced me individually believe it from numerous varied angles.

    Its like men and women are not interested unless it is something to do with Lady gaga!
    Your own stuffs outstanding. Always handle it up!

  7. 11 kwietnia 2013 o 3:05

    Write more, thats all I have to say. Literally, it seems as
    though you relied on the video to make your point.
    You definitely know what youre talking about, why waste your
    intelligence on just posting videos to your blog when you could be giving us
    something enlightening to read?

  8. 13 kwietnia 2013 o 6:36

    I love it when folks come together and share ideas. Great site, keep
    it up!

  9. 13 kwietnia 2013 o 20:22

    I really love your website.. Pleasant colors & theme. Did you
    make this website yourself? Please reply back as I’m hoping to create my own website and want to know where you got this from or what the theme is named. Thanks!

  10. 13 kwietnia 2013 o 20:41

    I pay a visit each day a few blogs and blogs to read content, except
    this webpage presents quality based writing.

  11. 14 kwietnia 2013 o 3:23

    We stumbled over here from a different website and thought I should check things out.
    I like what I see so now i am following you. Look forward to
    checking out your web page again.

  12. 21 kwietnia 2013 o 4:26

    This is a topic which is near to my heart… Best wishes!
    Where are your contact details though?

  13. 21 Maj 2013 o 7:35

    Great beat ! I would like to apprentice whilst you amend your web site, how can i subscribe for a blog website?

    The account helped me a acceptable deal. I were a little bit familiar
    of this your broadcast offered bright clear idea

  14. 2 czerwca 2013 o 2:10

    The clownfish are hermaphrodites, which means they develop into males
    first, and when they mature, they tend to change as females.
    There should be live rock and sand inside the tank. The less
    intense light they have, such as a fluorescent light, the less amount of algae will grow on the glass and decorations.

  15. 6 czerwca 2013 o 22:41

    Brainpulse is a top SEO Company India, which ensures
    that the search engine rankings of the web page fall within the top 10.
    Look for a company that makes a certain amount of effort in knowing your business, what you wish to achieve from your web presence etc.
    This has said to be effective in reducing paper
    waste by 90%.

  16. 30 czerwca 2013 o 14:54

    howdy! , I like the creating hence a whole lot! discuss most people talk more about this page in America online? I would like a professional within this area to uncover this challenge. Maybe which is anyone! Eager for help you.

  17. 9 lipca 2013 o 23:59

    What’s up everyone, it’s my first pay a quick visit at this site,
    and piece of writing is in fact fruitful designed for me, keep up posting these content.

  18. 21 lipca 2013 o 16:50

    People go for crash dieting is bad in order to function
    normally. As for trends in Dieting Is Bad, it’s hard to know what’s truly changing here.
    This is TRUE because the body is sent into starvation mode because it thinks
    it’s in a period of time. Before, I used Spark People, which is said to prolong health, stave off diagnosis, and it would be almost impossible to drop out.

  19. 12 sierpnia 2013 o 3:03

    Woah! I’m really enjoying the template/theme of this site. It’s
    simple, yet effective. A lot of times it’s challenging to get that „perfect balance” between user friendliness and visual appeal. I must say you’ve
    done a superb job with this. Additionally, the blog loads super fast for me on Internet explorer.
    Excellent Blog!

  20. 28 marca 2014 o 22:10

    Hi! This post could not be written any better! Reading through this post reminds me of my previous room
    mate! He always kept talking about this. I will forward
    this page to him. Pretty sure he will have a good read. Thanks for
    sharing!

  21. 14 kwietnia 2014 o 17:53

    A person essentially assist to make critically
    articles I might state. That is the very first time I frequented your web
    page and up to now? I surprised with the analysis you made to make
    this actual put up amazing. Great process!

  22. 23 kwietnia 2014 o 18:32

    Hi there, after reading this amazing piece of writing i am
    too happy to share my knowledge here with friends.

  23. 18 Maj 2014 o 4:53

    If you elect to download iPhone flick you’ll need MP4 codec.
    You can jailbreak iOS 4.0.2 via Redsn0w and 4.0.1 can be jailbroken via jailbreakme 2.0.

    Generally there is more than one strategy to jailbreak.

  24. 20 Maj 2014 o 5:06

    Some could be presented as presents out of your large neighbors.
    Cultural Location on Facebook is just a simple
    online game much like Simcity. But it is only recommended by me for adventurous and the courageous.

  25. 24 Maj 2014 o 22:29

    For more farming fun: click the link a subscription to my posts.
    Both Cash Puppies come with a few rules that don’t connect with
    their Plantation Cash competitors. Let’s possess a glance at the things they are.

  26. 27 Maj 2014 o 5:22

    Just stated repositories are sources or spots where you could download software
    applications. Is your iPhone Mobile 4 caught with the
    spinning wheel to the Apple logo?

  27. 30 Maj 2014 o 0:34

    This game is a must for Boggle or Scrabble lovers. Enabling this feature will permit you
    to instantly reply with out quitting the existing page.
    You can easily find it in iTunes Application Store.

  28. 30 Maj 2014 o 7:17

    These games could teach them anything or two, even though
    they don’t understand it. Make sure most of the routes are very different however the treasure in the long run is the
    same.

  29. 30 Maj 2014 o 16:18

    One of the tools therefore mentioned is the jailbreak application that was lamera1n. You could add (or remove) repos within Cydia.
    This series of city places would be the campaign’s beginning.

  30. 31 Maj 2014 o 8:17

    Within this criticism we suppose that you pull straight from lifestyle or from the suitable
    image. Do not overlook, the arabesque includes the whole
    of the top such as the hair.

  31. 10 czerwca 2014 o 14:42

    Hello there! This is my first comment here so I just wanted to give
    a quick shout out and say I genuinely enjoy reading through your
    articles. Can you suggest any other blogs/websites/forums that
    cover the same topics? Thanks for your time!

  32. 30 lipca 2014 o 17:10

    Great article.

  33. 5 sierpnia 2014 o 17:14

    Nice post. I learn something totally new and challenging on websites
    I stumbleupon everyday. It will always be helpful to read through content from other authors and practice something from their sites.

  34. 6 sierpnia 2014 o 10:56

    Toller Post. Genau so unterhaltsam, wie das Gaming auch.
    So verrissen Computerspiele auch hingestellt wird, so ist psychologisch bewiesen worden, dass Gaming, ein dem Alter des
    Gamers angemessenes Game vorausgesetzt, förderlich
    für das Reaktionsvermögen ist. Beim Spielen lernt der Gamer
    fernab vom Alltag in erhöhter Frequenz zu Reagieren und zwischen wichtig und unwichtig zu unterscheiden. Viele
    Spiele vermitteln zusätzlich noch Kenntnisse über Wirtschaftlichkeit und fördern die Logik.
    Auch die verpönten MMOs können doch häufig den vorgeworfenen Effekt
    des Abdriftens in eine virtuelle Realität} aufhalten. Der Spieler mag mitunter vielen Freaks begegnen, jedoch findet der Spieler häufig in seinem Lieblingsspiel Gleichgesinnte.

    Kurz gehalten: das Gaming ist das Beste überhaupt!
    Ungeachtet des Games und der Plattform, Spielen ist ein Kulturgut.

  35. 10 sierpnia 2014 o 17:30

    Light this time is reflected on inclusions positioned parallel with
    each other. The shapes of designs also have meanings and
    symbolism such as the elephant with wealth and luck.
    Chrysoberyl This gemstone is arguably the most popular gemstone for its cats
    eye inclusions.

  36. 17 października 2014 o 18:20

    When someone writes an article he/she maintains
    the idea of a user in his/her mind that how a user can be aware
    of it. Thus that’s why this post is perfect.
    Thanks!

  37. 22 października 2014 o 16:59

    Nowadays the SEO industry gets a bad reputation from the amount of snake oil salesmen and overseas companies currently set
    up
    Bookmarked your blog, heres to hoping you keep up to date with regular posts

  38. 2 kwietnia 2015 o 16:18

    Excellent beat ! I would like to apprentice at the
    same time as you amend your web site, how can i subscribe for a
    blog site? The account aided me a applicable deal. I had been a
    little bit familiar of this your broadcast offered shiny clear concept

  39. 4 Maj 2015 o 23:23

    It’s actually a nice and useful piece of info. I’m satisfied that you just shared this useful info with us.

    Please keep us up to date like this. Thanks for sharing.

  40. 3 lipca 2015 o 9:16

    Yes! Finally someon writes aƄoսt tethered jealbreak.

  41. 4 czerwca 2016 o 7:16

    どうすればプロミスの審査に通るのか?審査を通してすぐキャッシングする方法を大公開!

  42. 19 czerwca 2016 o 1:09

    即日の融資が希望の時には、財務状況の良い融資会社を活用することが大切です。実際に利用してみると、役に立つ取引を受けることができます。業者を選ぶ時は、慎重に申し込むことが大切です。

  43. 22 września 2016 o 11:20

    脱毛はしてみたいけど…という沖縄のあなた!
    痛いのはイヤ!冷たいジェルもイヤ!予約が取りづらいのもイヤ!
    こんな心配が全部解決されていて、さらに脱毛と同時にスリムボディーも目指せてしまう脱毛サロンがあるとしたら、ちょっと気になりませんか?
    沖縄でいろいろ検証してみて、私が見つけたおススメの脱毛サロンはこちらです。
    全身脱毛が私の一押しです。

  44. 3 października 2016 o 5:29

    Is there a benefit in employing someone with experience to carry out the link exchanges, or
    do you do it yourself?
    Will surely be coming back, ’tis a great site!

  45. 15 stycznia 2017 o 3:19

    Today, I went to the beachfront with my kids.

    I found a sea shell and gave it to my 4 year old daughter and said „You can hear the ocean if you put this to your ear.” She put the shell to her ear
    and screamed. There was a hermit crab inside and it pinched her ear.
    She never wants to go back! LoL I know this is entirely off
    topic but I had to tell someone!

  46. 10 lutego 2017 o 2:19

    脱毛エステが増加したとは言え、割安で、技術レベルも高い著名な脱毛エステに決めた場合、キャンセル待ちをしている人もいるので、すんなりと予約が取り辛いというケースも少なくありません。
    脱毛サロンにおいては、脱毛に取り掛かる前にカウンセリングをしますが、この時間内でのスタッフの説明などが無礼極まりない脱毛サロンである場合、技能も劣るというふうに考えて間違いないでしょう。
    絶対VIO脱毛は無理と言われる人は、4回コースや6回コースといった複数回コースを頼むのではなく、とりあえず1回だけ行なってみてはと言いたいです。
    「永久脱毛をやりたい」と思ったのなら、最優先で意思決定しなければいけないのが、サロンで行うかクリニックで行うかということですね。双方には脱毛方法に差異があります。
    沖縄のお勧め脱毛サロン情報はこのサイトにもありますよ。

  47. 1 Maj 2017 o 7:11

    Its not my first time to pay a quick visit this web page, i am visiting this website dailly and obtain good data from here everyday.

  48. 14 Maj 2017 o 19:33

    What’s up to every body, it’s my first pay a quick visit of
    this webpage; this web site contains amazing and in fact good data in support of visitors.

  49. 2 czerwca 2017 o 5:50

    Very good post. I definitely love this website.
    Stick with it!

  50. 19 lipca 2017 o 21:59

    Magnificent website. A lot of helpful information here.
    I’m sending it to several buddies ans additionally sharing in delicious.
    And obviously, thanks in your effort!

  51. 8 września 2017 o 5:31

    My brother suggested I would possibly like this website.

    He was once totally right. This publish actually made my
    day. You cann’t consider simply how so much time I had spent for this info!
    Thank you!

  1. 7 listopada 2014 o 9:52
  2. 26 grudnia 2016 o 19:26
  3. 30 grudnia 2016 o 20:32
  4. 3 stycznia 2017 o 21:14
  5. 3 stycznia 2017 o 21:17
  6. 4 stycznia 2017 o 22:03
  7. 5 stycznia 2017 o 20:27
  8. 8 stycznia 2017 o 16:33
  9. 8 stycznia 2017 o 19:30
  10. 12 stycznia 2017 o 11:20
  11. 12 stycznia 2017 o 12:34
  12. 4 marca 2017 o 0:05
  13. 11 czerwca 2017 o 8:44
  14. 16 lipca 2017 o 12:20

Skomentuj

Wprowadź swoje dane lub kliknij jedną z tych ikon, aby się zalogować:

Logo WordPress.com

Komentujesz korzystając z konta WordPress.com. Wyloguj / Zmień )

Zdjęcie z Twittera

Komentujesz korzystając z konta Twitter. Wyloguj / Zmień )

Zdjęcie na Facebooku

Komentujesz korzystając z konta Facebook. Wyloguj / Zmień )

Zdjęcie na Google+

Komentujesz korzystając z konta Google+. Wyloguj / Zmień )

Connecting to %s

%d blogerów lubi to: