将任何标题转换为 url slug,然后从 url slug 转换回标题

发布于 2024-10-08 21:35:50 字数 427 浏览 4 评论 0原文

我想将博客条目的任何标题转换为用户友好的网址。我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

喵星人汪星人 2024-10-15 21:35:50

您想要创建段头,但根据经验,我可以告诉您解码的可能性是有限的。例如,“Foo - Bar”将变成“foo-bar”,那么你怎么可能知道它一直不是“foo bar”或“foo-bar”呢?

或者你不想在你的 slug 中使用并且也没有像“`”这样的表示的字符怎么样?
因此,您可以使用像 rawurlencode() 这样的 1 到 1 转换,或者您可以创建一个 Slug,这里是一个函数的示例 - 但正如我所说,不可能进行可靠的解码 - 它只是在它的自然,因为你必须丢弃信息。

function sanitizeStringForUrl($string){
    $string = strtolower($string);
    $string = html_entity_decode($string);
    $string = str_replace(array('ä','ü','ö','ß'),array('ae','ue','oe','ss'),$string);
    $string = preg_replace('#[^\w\säüöß]#',null,$string);
    $string = preg_replace('#[\s]{2,}#',' ',$string);
    $string = str_replace(array(' '),array('-'),$string);
    return $string;
}

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.

function sanitizeStringForUrl($string){
    $string = strtolower($string);
    $string = html_entity_decode($string);
    $string = str_replace(array('ä','ü','ö','ß'),array('ae','ue','oe','ss'),$string);
    $string = preg_replace('#[^\w\säüöß]#',null,$string);
    $string = preg_replace('#[\s]{2,}#',' ',$string);
    $string = str_replace(array(' '),array('-'),$string);
    return $string;
}
つ可否回来 2024-10-15 21:35:50
function url_title($str, $separator = 'dash', $lowercase = FALSE)
 {
  if ($separator == 'dash')
  {
   $search  = '_';
   $replace = '-';
  }
  else
  {
   $search  = '-';
   $replace = '_';
  }

  $trans = array(
      '&\#\d+?;'    => '',
      '&\S+?;'    => '',
      '\s+'     => $replace,
      '[^a-z0-9\-\._]'  => '',
      $replace.'+'   => $replace,
      $replace.'
   => $replace,
      '^'.$replace   => $replace,
      '\.+
     => ''
       );

  $str = strip_tags($str);

  foreach ($trans as $key => $val)
  {
   $str = preg_replace("#".$key."#i", $val, $str);
  }

  if ($lowercase === TRUE)
  {
   $str = strtolower($str);
  }

  return trim(stripslashes($str));
 }
function url_title($str, $separator = 'dash', $lowercase = FALSE)
 {
  if ($separator == 'dash')
  {
   $search  = '_';
   $replace = '-';
  }
  else
  {
   $search  = '-';
   $replace = '_';
  }

  $trans = array(
      '&\#\d+?;'    => '',
      '&\S+?;'    => '',
      '\s+'     => $replace,
      '[^a-z0-9\-\._]'  => '',
      $replace.'+'   => $replace,
      $replace.'
   => $replace,
      '^'.$replace   => $replace,
      '\.+
     => ''
       );

  $str = strip_tags($str);

  foreach ($trans as $key => $val)
  {
   $str = preg_replace("#".$key."#i", $val, $str);
  }

  if ($lowercase === TRUE)
  {
   $str = strtolower($str);
  }

  return trim(stripslashes($str));
 }
迷途知返 2024-10-15 21:35:50

我认为最优雅的方式是使用 Behat\Transliterator\Transliterator。

我需要通过你的类扩展这个类,因为它是一个抽象,有些像这样:

<?php
use Behat\Transliterator\Transliterator;

class Urlizer extends Transliterator
{
}

然后,只需使用它:

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

当然你也应该把这些东西放在你的作曲家中。

composer require behat/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:

<?php
use Behat\Transliterator\Transliterator;

class Urlizer extends Transliterator
{
}

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

万人眼中万个我 2024-10-15 21:35:50

没有可靠的方法可以将弹头“解码”回其原始形式。最好的解决方案是将 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文