Convert any title to url slug and back from url slug to title

前端 未结 3 1685
南旧
南旧 2021-02-14 21:43

I want to convert any title e.g. of a blog entry to a user friendly url. I used rawurlencode() to do that but it gives me a lot of strange strings like %s.

3条回答
  •  迷失自我
    2021-02-14 22:26

    The most elegant way I think is using a Behat\Transliterator\Transliterator.

    I need to extends this class by your class because it is an Abstract, some like this:

    And then, just use it:

    $text = "Master Ápiu";
    $urlizer = new Urlizer();
    $slug = $urlizer->transliterate($slug, "-");
    echo $slug; // master-apiu
    

    Of course you should put this things in your composer as well.

    composer require behat/transliterator
    

    More info here https://github.com/Behat/Transliterator

提交回复
热议问题