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

前端 未结 3 1785
梦谈多话
梦谈多话 2021-02-14 21:47

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:15

    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

提交回复
热议问题