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

前端 未结 3 1692
南旧
南旧 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:17

    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;
    }
    
    0 讨论(0)
  • 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:

    <?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

    0 讨论(0)
  • 2021-02-14 22:36
    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));
     }
    
    0 讨论(0)
提交回复
热议问题