将任何标题转换为 url slug,然后从 url slug 转换回标题
我想将博客条目的任何标题转换为用户友好的网址。我使用 rawurlencode() 来做到这一点,但它给了我很多奇怪的字符串,例如 % s
。
该算法应考虑德语字符,如 Ö、ä 等。我想从标题创建一个 url,并能够通过解码 url 来获取标题。
我尝试了一些其他问题中提供的代码: http://pastebin.com/L1SwESBn 但这似乎是一种方式。
例如 HÖRZU.de ->霍尔祖德-> HÖRZU.de
有什么想法吗?
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
.
The algorithm should consider german chars like Ö, Ä, etc. I want to make a url from title and be able to get the title by decoding the url.
I tried some of this code: http://pastebin.com/L1SwESBn that is provided in some other questions but it seems to be one way.
E.g. HÖRZU.de -> hoerzu-de -> HÖRZU.de
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您想要创建段头,但根据经验,我可以告诉您解码的可能性是有限的。例如,“Foo - Bar”将变成“foo-bar”,那么你怎么可能知道它一直不是“foo bar”或“foo-bar”呢?
或者你不想在你的 slug 中使用并且也没有像“`”这样的表示的字符怎么样?
因此,您可以使用像
rawurlencode()
这样的 1 到 1 转换,或者您可以创建一个 Slug,这里是一个函数的示例 - 但正如我所说,不可能进行可靠的解码 - 它只是在它的自然,因为你必须丢弃信息。You want to create slugs, but from experience i can tell you the decode possibilities are limited. For example "Foo - Bar" will become "foo-bar" so how do you then can possibly know that it wasn't "foo bar" or "foo-bar" all along?
Or how about chars that you don't want in your slug and also have no representation for like " ` "?
So you can ether use a 1 to 1 converstion like
rawurlencode()
or you can create a Slug, here is an example for a function - but as i said, no reliable decoding possible - its just in its nature since you have to throw away Information.我认为最优雅的方式是使用 Behat\Transliterator\Transliterator。
我需要通过你的类扩展这个类,因为它是一个抽象,有些像这样:
然后,只需使用它:
当然你也应该把这些东西放在你的作曲家中。
更多信息请点击这里 https://github.com/Behat/Transliterator
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:
Of course you should put this things in your composer as well.
More info here https://github.com/Behat/Transliterator
没有可靠的方法可以将弹头“解码”回其原始形式。最好的解决方案是将 slug 及其原始数据存储在数据库中。
there is no reliable way to 'decode' the slug back to its original form. the best solution here would be to database the slug and its original.