代码之家  ›  专栏  ›  技术社区  ›  Andres SK

php函数生成slug(url字符串)

  •  133
  • Andres SK  · 技术社区  · 15 年前
    function gen_slug($str){
        # special accents
        $a = array('À','Á','Â','Ã','Ä','Å','Æ','Ç','È','É','Ê','Ë','Ì','Í','Î','Ï','Ð','Ñ','Ò','Ó','Ô','Õ','Ö','Ø','Ù','Ú','Û','Ü','Ý','ß','à','á','â','ã','ä','å','æ','ç','è','é','ê','ë','ì','í','î','ï','ñ','ò','ó','ô','õ','ö','ø','ù','ú','û','ü','ý','ÿ','A','a','A','a','A','a','C','c','C','c','C','c','C','c','D','d','Ð','d','E','e','E','e','E','e','E','e','E','e','G','g','G','g','G','g','G','g','H','h','H','h','I','i','I','i','I','i','I','i','I','i','?','?','J','j','K','k','L','l','L','l','L','l','?','?','L','l','N','n','N','n','N','n','?','O','o','O','o','O','o','Œ','œ','R','r','R','r','R','r','S','s','S','s','S','s','Š','š','T','t','T','t','T','t','U','u','U','u','U','u','U','u','U','u','U','u','W','w','Y','y','Ÿ','Z','z','Z','z','Ž','ž','?','ƒ','O','o','U','u','A','a','I','i','O','o','U','u','U','u','U','u','U','u','U','u','?','?','?','?','?','?');
        $b = array('A','A','A','A','A','A','AE','C','E','E','E','E','I','I','I','I','D','N','O','O','O','O','O','O','U','U','U','U','Y','s','a','a','a','a','a','a','ae','c','e','e','e','e','i','i','i','i','n','o','o','o','o','o','o','u','u','u','u','y','y','A','a','A','a','A','a','C','c','C','c','C','c','C','c','D','d','D','d','E','e','E','e','E','e','E','e','E','e','G','g','G','g','G','g','G','g','H','h','H','h','I','i','I','i','I','i','I','i','I','i','IJ','ij','J','j','K','k','L','l','L','l','L','l','L','l','l','l','N','n','N','n','N','n','n','O','o','O','o','O','o','OE','oe','R','r','R','r','R','r','S','s','S','s','S','s','S','s','T','t','T','t','T','t','U','u','U','u','U','u','U','u','U','u','U','u','W','w','Y','y','Y','Z','z','Z','z','Z','z','s','f','O','o','U','u','A','a','I','i','O','o','U','u','U','u','U','u','U','u','U','u','A','a','AE','ae','O','o');
        return strtolower(preg_replace(array('/[^a-zA-Z0-9 -]/','/[ -]+/','/^-|-$/'),array('','-',''),str_replace($a,$b,$str)));
    }
    

    效果很好,但我发现了一些失败的案例:

    gen_slug('andrés') 收益率 andras 而不是 andres

    为什么?有什么想法吗 preg_replace 参数?

    19 回复  |  直到 6 年前
        1
  •  378
  •   leoap    6 年前

    不要用冗长的替换,试试这个:

    public static function slugify($text)
    {
      // replace non letter or digits by -
      $text = preg_replace('~[^\pL\d]+~u', '-', $text);
    
      // transliterate
      $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
    
      // remove unwanted characters
      $text = preg_replace('~[^-\w]+~', '', $text);
    
      // trim
      $text = trim($text, '-');
    
      // remove duplicate -
      $text = preg_replace('~-+~', '-', $text);
    
      // lowercase
      $text = strtolower($text);
    
      if (empty($text)) {
        return 'n-a';
      }
    
      return $text;
    }
    

    这是基于symfony的Jobeet教程。

        2
  •  36
  •   TheKalpit    6 年前

    更新

    由于这个答案引起了人们的注意,我正在添加一些解释。

    提供的解决方案将基本上用-(连字符)替换除A-Z、A-Z、0-9、&-(连字符)之外的所有内容。因此,它不能与其他Unicode字符(对于URL slug/string是有效字符)一起正常工作。常见的情况是输入字符串包含非英语字符。

    只有当您确信输入字符串不包含您可能希望成为输出/段塞一部分的Unicode字符时,才使用此解决方案。

    例如,“_·_·_·_·_·_·_·_·_·_·_·_·_”将变为“-----”(所有连字符),而不是“_·_·_·_·_·_·_·_·_·_·_·_”(有效的URL段塞)。

    原始答案

    怎么样。。。

    $slug = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string)));
    

    ?

        3
  •  31
  •   hdogan    12 年前

    如果你有 intl 已安装扩展,可以使用 transliterator_transliterate 函数可以轻松创建段塞。

    以后可以用破折号替换空格,使其更像一个段塞。

    <?php
    $string = "andrés";
    $string = transliterator_transliterate("Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove; Lower();", $string);
    echo $string;
    ?>
    
        4
  •  22
  •   Imran Omar Bukhsh    11 年前

    注意:我从WordPress上取下了这个,它工作了!!

    这样使用:

    echo sanitize('testing this link');
    

    代码

    //taken from wordpress
    function utf8_uri_encode( $utf8_string, $length = 0 ) {
        $unicode = '';
        $values = array();
        $num_octets = 1;
        $unicode_length = 0;
    
        $string_length = strlen( $utf8_string );
        for ($i = 0; $i < $string_length; $i++ ) {
    
            $value = ord( $utf8_string[ $i ] );
    
            if ( $value < 128 ) {
                if ( $length && ( $unicode_length >= $length ) )
                    break;
                $unicode .= chr($value);
                $unicode_length++;
            } else {
                if ( count( $values ) == 0 ) $num_octets = ( $value < 224 ) ? 2 : 3;
    
                $values[] = $value;
    
                if ( $length && ( $unicode_length + ($num_octets * 3) ) > $length )
                    break;
                if ( count( $values ) == $num_octets ) {
                    if ($num_octets == 3) {
                        $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]) . '%' . dechex($values[2]);
                        $unicode_length += 9;
                    } else {
                        $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]);
                        $unicode_length += 6;
                    }
    
                    $values = array();
                    $num_octets = 1;
                }
            }
        }
    
        return $unicode;
    }
    
    //taken from wordpress
    function seems_utf8($str) {
        $length = strlen($str);
        for ($i=0; $i < $length; $i++) {
            $c = ord($str[$i]);
            if ($c < 0x80) $n = 0; # 0bbbbbbb
            elseif (($c & 0xE0) == 0xC0) $n=1; # 110bbbbb
            elseif (($c & 0xF0) == 0xE0) $n=2; # 1110bbbb
            elseif (($c & 0xF8) == 0xF0) $n=3; # 11110bbb
            elseif (($c & 0xFC) == 0xF8) $n=4; # 111110bb
            elseif (($c & 0xFE) == 0xFC) $n=5; # 1111110b
            else return false; # Does not match any model
            for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
                if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80))
                    return false;
            }
        }
        return true;
    }
    
    //function sanitize_title_with_dashes taken from wordpress
    function sanitize($title) {
        $title = strip_tags($title);
        // Preserve escaped octets.
        $title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
        // Remove percent signs that are not part of an octet.
        $title = str_replace('%', '', $title);
        // Restore octets.
        $title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);
    
        if (seems_utf8($title)) {
            if (function_exists('mb_strtolower')) {
                $title = mb_strtolower($title, 'UTF-8');
            }
            $title = utf8_uri_encode($title, 200);
        }
    
        $title = strtolower($title);
        $title = preg_replace('/&.+?;/', '', $title); // kill entities
        $title = str_replace('.', '-', $title);
        $title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
        $title = preg_replace('/\s+/', '-', $title);
        $title = preg_replace('|-+|', '-', $title);
        $title = trim($title, '-');
    
        return $title;
    }
    
        5
  •  8
  •   Baptiste Gaillard    12 年前

    这里还有另一个,例如,“带有奇怪字符的标题”a x z“变成了“带有奇怪字符的标题”eee-a-x-z。

    /**
     * Function used to create a slug associated to an "ugly" string.
     *
     * @param string $string the string to transform.
     *
     * @return string the resulting slug.
     */
    public static function createSlug($string) {
    
        $table = array(
                'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
                'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
                'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
                'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
                'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
                'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
                'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
                'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r', '/' => '-', ' ' => '-'
        );
    
        // -- Remove duplicated spaces
        $stripped = preg_replace(array('/\s{2,}/', '/[\t\n]/'), ' ', $string);
    
        // -- Returns the slug
        return strtolower(strtr($string, $table));
    
    
    }
    
        6
  •  8
  •   czerasz    10 年前

    @imran omar bukhsh代码的更新版本(来自最新的Wordpress(4.0)分支):

    <?php
    
    // Add methods to slugify taken from Wordpress:
    // - https://github.com/WordPress/WordPress/blob/master/wp-includes/formatting.php 
    // - https://github.com/WordPress/WordPress/blob/master/wp-includes/functions.php
    
    /**
     * Set the mbstring internal encoding to a binary safe encoding when func_overload
     * is enabled.
     *
     * When mbstring.func_overload is in use for multi-byte encodings, the results from
     * strlen() and similar functions respect the utf8 characters, causing binary data
     * to return incorrect lengths.
     *
     * This function overrides the mbstring encoding to a binary-safe encoding, and
     * resets it to the users expected encoding afterwards through the
     * `reset_mbstring_encoding` function.
     *
     * It is safe to recursively call this function, however each
     * `mbstring_binary_safe_encoding()` call must be followed up with an equal number
     * of `reset_mbstring_encoding()` calls.
     *
     * @since 3.7.0
     *
     * @see reset_mbstring_encoding()
     *
     * @param bool $reset Optional. Whether to reset the encoding back to a previously-set encoding.
     *                    Default false.
     */
    function mbstring_binary_safe_encoding( $reset = false ) {
      static $encodings = array();
      static $overloaded = null;
    
      if ( is_null( $overloaded ) )
        $overloaded = function_exists( 'mb_internal_encoding' ) && ( ini_get( 'mbstring.func_overload' ) & 2 );
    
      if ( false === $overloaded )
        return;
    
      if ( ! $reset ) {
        $encoding = mb_internal_encoding();
        array_push( $encodings, $encoding );
        mb_internal_encoding( 'ISO-8859-1' );
      }
    
      if ( $reset && $encodings ) {
        $encoding = array_pop( $encodings );
        mb_internal_encoding( $encoding );
      }
    }
    
    /**
     * Reset the mbstring internal encoding to a users previously set encoding.
     *
     * @see mbstring_binary_safe_encoding()
     *
     * @since 3.7.0
     */
    function reset_mbstring_encoding() {
      mbstring_binary_safe_encoding( true );
    }
    
    
    /**
     * Checks to see if a string is utf8 encoded.
     *
     * NOTE: This function checks for 5-Byte sequences, UTF8
     *       has Bytes Sequences with a maximum length of 4.
     *
     * @author bmorel at ssi dot fr (modified)
     * @since 1.2.1
     *
     * @param string $str The string to be checked
     * @return bool True if $str fits a UTF-8 model, false otherwise.
     */
    function seems_utf8($str) {
      mbstring_binary_safe_encoding();
      $length = strlen($str);
      reset_mbstring_encoding();
      for ($i=0; $i < $length; $i++) {
        $c = ord($str[$i]);
        if ($c < 0x80) $n = 0; # 0bbbbbbb
        elseif (($c & 0xE0) == 0xC0) $n=1; # 110bbbbb
        elseif (($c & 0xF0) == 0xE0) $n=2; # 1110bbbb
        elseif (($c & 0xF8) == 0xF0) $n=3; # 11110bbb
        elseif (($c & 0xFC) == 0xF8) $n=4; # 111110bb
        elseif (($c & 0xFE) == 0xFC) $n=5; # 1111110b
        else return false; # Does not match any model
        for ($j=0; $j<$n; $j++) { # n bytes matching 10bbbbbb follow ?
          if ((++$i == $length) || ((ord($str[$i]) & 0xC0) != 0x80))
            return false;
        }
      }
      return true;
    }
    
    
    /**
     * Encode the Unicode values to be used in the URI.
     *
     * @since 1.5.0
     *
     * @param string $utf8_string
     * @param int $length Max length of the string
     * @return string String with Unicode encoded for URI.
     */
    function utf8_uri_encode( $utf8_string, $length = 0 ) {
      $unicode = '';
      $values = array();
      $num_octets = 1;
      $unicode_length = 0;
    
      mbstring_binary_safe_encoding();
      $string_length = strlen( $utf8_string );
      reset_mbstring_encoding();
    
      for ($i = 0; $i < $string_length; $i++ ) {
    
        $value = ord( $utf8_string[ $i ] );
    
        if ( $value < 128 ) {
          if ( $length && ( $unicode_length >= $length ) )
            break;
          $unicode .= chr($value);
          $unicode_length++;
        } else {
          if ( count( $values ) == 0 ) $num_octets = ( $value < 224 ) ? 2 : 3;
    
          $values[] = $value;
    
          if ( $length && ( $unicode_length + ($num_octets * 3) ) > $length )
            break;
          if ( count( $values ) == $num_octets ) {
            if ($num_octets == 3) {
              $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]) . '%' . dechex($values[2]);
              $unicode_length += 9;
            } else {
              $unicode .= '%' . dechex($values[0]) . '%' . dechex($values[1]);
              $unicode_length += 6;
            }
    
            $values = array();
            $num_octets = 1;
          }
        }
      }
    
      return $unicode;
    }
    
    
    /**
     * Sanitizes a title, replacing whitespace and a few other characters with dashes.
     *
     * Limits the output to alphanumeric characters, underscore (_) and dash (-).
     * Whitespace becomes a dash.
     *
     * @since 1.2.0
     *
     * @param string $title The title to be sanitized.
     * @param string $raw_title Optional. Not used.
     * @param string $context Optional. The operation for which the string is sanitized.
     * @return string The sanitized title.
     */
    function sanitize_title_with_dashes( $title, $raw_title = '', $context = 'display' ) {
      $title = strip_tags($title);
      // Preserve escaped octets.
      $title = preg_replace('|%([a-fA-F0-9][a-fA-F0-9])|', '---$1---', $title);
      // Remove percent signs that are not part of an octet.
      $title = str_replace('%', '', $title);
      // Restore octets.
      $title = preg_replace('|---([a-fA-F0-9][a-fA-F0-9])---|', '%$1', $title);
    
      if (seems_utf8($title)) {
        if (function_exists('mb_strtolower')) {
          $title = mb_strtolower($title, 'UTF-8');
        }
        $title = utf8_uri_encode($title, 200);
      }
    
      $title = strtolower($title);
      $title = preg_replace('/&.+?;/', '', $title); // kill entities
      $title = str_replace('.', '-', $title);
    
      if ( 'save' == $context ) {
        // Convert nbsp, ndash and mdash to hyphens
        $title = str_replace( array( '%c2%a0', '%e2%80%93', '%e2%80%94' ), '-', $title );
    
        // Strip these characters entirely
        $title = str_replace( array(
          // iexcl and iquest
          '%c2%a1', '%c2%bf',
          // angle quotes
          '%c2%ab', '%c2%bb', '%e2%80%b9', '%e2%80%ba',
          // curly quotes
          '%e2%80%98', '%e2%80%99', '%e2%80%9c', '%e2%80%9d',
          '%e2%80%9a', '%e2%80%9b', '%e2%80%9e', '%e2%80%9f',
          // copy, reg, deg, hellip and trade
          '%c2%a9', '%c2%ae', '%c2%b0', '%e2%80%a6', '%e2%84%a2',
          // acute accents
          '%c2%b4', '%cb%8a', '%cc%81', '%cd%81',
          // grave accent, macron, caron
          '%cc%80', '%cc%84', '%cc%8c',
        ), '', $title );
    
        // Convert times to x
        $title = str_replace( '%c3%97', 'x', $title );
      }
    
      $title = preg_replace('/[^%a-z0-9 _-]/', '', $title);
      $title = preg_replace('/\s+/', '-', $title);
      $title = preg_replace('|-+|', '-', $title);
      $title = trim($title, '-');
    
      return $title;
    }
    
    $title = '#PFW Alexander McQueen Spring/Summer 2015';
    echo "title -> slug: \n". $title ." -> ". sanitize_title_with_dashes($title);
    echo "\n\n";
    $title = '«GQ»: Elyas M\'Barek gehört zu Männern des Jahres';
    echo "title -> slug: \n". $title ." -> ". sanitize_title_with_dashes($title);
    

    在线查看 example .

        7
  •  5
  •   Vazgen Manukyan    9 年前

    使用许多高级开发人员支持的现有解决方案总是一个好主意。最受欢迎的是 https://github.com/cocur/slugify . 首先,它支持多种语言,并且正在更新。

    如果您不想使用整个包,您只需复制所需的部分即可。

        8
  •  4
  •   Entendu    15 年前

    不要用preg_替换。有一个专门为任务构建的PHP函数:strtr()。 http://php.net/manual/en/function.strtr.php

    摘自上述链接中的评论(我自己测试过;它工作:

    function normalize ($string) {
        $table = array(
            'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
            'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
            'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
            'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
            'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
            'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
            'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
            'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r',
        );
    
        return strtr($string, $table);
    }
    
        9
  •  4
  •   Mladen Janjetovic    10 年前

    我正在使用:

    function slugify($text)
    { 
        $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
        return strtolower(preg_replace('/[^A-Za-z0-9-]+/', '-', $text));
    }
    

    唯一的回退是西里尔字符不会被转换,我现在正在寻找解决方案,它不是每个西里尔字符的长str_替换。

        10
  •  4
  •   Nady Shalaby    9 年前
    public static function slugify ($text) {
    
        $replace = [
            '&lt;' => '', '&gt;' => '', '&#039;' => '', '&amp;' => '',
            '&quot;' => '', 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä'=> 'Ae',
            '&Auml;' => 'A', 'Å' => 'A', 'Ā' => 'A', 'Ą' => 'A', 'Ă' => 'A', 'Æ' => 'Ae',
            'Ç' => 'C', 'Ć' => 'C', 'Č' => 'C', 'Ĉ' => 'C', 'Ċ' => 'C', 'Ď' => 'D', 'Đ' => 'D',
            'Ð' => 'D', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ē' => 'E',
            'Ę' => 'E', 'Ě' => 'E', 'Ĕ' => 'E', 'Ė' => 'E', 'Ĝ' => 'G', 'Ğ' => 'G',
            'Ġ' => 'G', 'Ģ' => 'G', 'Ĥ' => 'H', 'Ħ' => 'H', 'Ì' => 'I', 'Í' => 'I',
            'Î' => 'I', 'Ï' => 'I', 'Ī' => 'I', 'Ĩ' => 'I', 'Ĭ' => 'I', 'Į' => 'I',
            'İ' => 'I', 'IJ' => 'IJ', 'Ĵ' => 'J', 'Ķ' => 'K', 'Ł' => 'K', 'Ľ' => 'K',
            'Ĺ' => 'K', 'Ļ' => 'K', 'Ŀ' => 'K', 'Ñ' => 'N', 'Ń' => 'N', 'Ň' => 'N',
            'Ņ' => 'N', 'Ŋ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O',
            'Ö' => 'Oe', '&Ouml;' => 'Oe', 'Ø' => 'O', 'Ō' => 'O', 'Ő' => 'O', 'Ŏ' => 'O',
            'Œ' => 'OE', 'Ŕ' => 'R', 'Ř' => 'R', 'Ŗ' => 'R', 'Ś' => 'S', 'Š' => 'S',
            'Ş' => 'S', 'Ŝ' => 'S', 'Ș' => 'S', 'Ť' => 'T', 'Ţ' => 'T', 'Ŧ' => 'T',
            'Ț' => 'T', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'Ue', 'Ū' => 'U',
            '&Uuml;' => 'Ue', 'Ů' => 'U', 'Ű' => 'U', 'Ŭ' => 'U', 'Ũ' => 'U', 'Ų' => 'U',
            'Ŵ' => 'W', 'Ý' => 'Y', 'Ŷ' => 'Y', 'Ÿ' => 'Y', 'Ź' => 'Z', 'Ž' => 'Z',
            'Ż' => 'Z', 'Þ' => 'T', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a',
            'ä' => 'ae', '&auml;' => 'ae', 'å' => 'a', 'ā' => 'a', 'ą' => 'a', 'ă' => 'a',
            'æ' => 'ae', 'ç' => 'c', 'ć' => 'c', 'č' => 'c', 'ĉ' => 'c', 'ċ' => 'c',
            'ď' => 'd', 'đ' => 'd', 'ð' => 'd', 'è' => 'e', 'é' => 'e', 'ê' => 'e',
            'ë' => 'e', 'ē' => 'e', 'ę' => 'e', 'ě' => 'e', 'ĕ' => 'e', 'ė' => 'e',
            'ƒ' => 'f', 'ĝ' => 'g', 'ğ' => 'g', 'ġ' => 'g', 'ģ' => 'g', 'ĥ' => 'h',
            'ħ' => 'h', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ī' => 'i',
            'ĩ' => 'i', 'ĭ' => 'i', 'į' => 'i', 'ı' => 'i', 'ij' => 'ij', 'ĵ' => 'j',
            'ķ' => 'k', 'ĸ' => 'k', 'ł' => 'l', 'ľ' => 'l', 'ĺ' => 'l', 'ļ' => 'l',
            'ŀ' => 'l', 'ñ' => 'n', 'ń' => 'n', 'ň' => 'n', 'ņ' => 'n', 'ʼn' => 'n',
            'ŋ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'oe',
            '&ouml;' => 'oe', 'ø' => 'o', 'ō' => 'o', 'ő' => 'o', 'ŏ' => 'o', 'œ' => 'oe',
            'ŕ' => 'r', 'ř' => 'r', 'ŗ' => 'r', 'š' => 's', 'ù' => 'u', 'ú' => 'u',
            'û' => 'u', 'ü' => 'ue', 'ū' => 'u', '&uuml;' => 'ue', 'ů' => 'u', 'ű' => 'u',
            'ŭ' => 'u', 'ũ' => 'u', 'ų' => 'u', 'ŵ' => 'w', 'ý' => 'y', 'ÿ' => 'y',
            'ŷ' => 'y', 'ž' => 'z', 'ż' => 'z', 'ź' => 'z', 'þ' => 't', 'ß' => 'ss',
            'ſ' => 'ss', 'ый' => 'iy', 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G',
            'Д' => 'D', 'Е' => 'E', 'Ё' => 'YO', 'Ж' => 'ZH', 'З' => 'Z', 'И' => 'I',
            'Й' => 'Y', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O',
            'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F',
            'Х' => 'H', 'Ц' => 'C', 'Ч' => 'CH', 'Ш' => 'SH', 'Щ' => 'SCH', 'Ъ' => '',
            'Ы' => 'Y', 'Ь' => '', 'Э' => 'E', 'Ю' => 'YU', 'Я' => 'YA', 'а' => 'a',
            'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'yo',
            'ж' => 'zh', 'з' => 'z', 'и' => 'i', 'й' => 'y', 'к' => 'k', 'л' => 'l',
            'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's',
            'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c', 'ч' => 'ch',
            'ш' => 'sh', 'щ' => 'sch', 'ъ' => '', 'ы' => 'y', 'ь' => '', 'э' => 'e',
            'ю' => 'yu', 'я' => 'ya'
        ];
    
        // make a human readable string
        $text = strtr($text, $replace);
    
        // replace non letter or digits by -
        $text = preg_replace('~[^\\pL\d.]+~u', '-', $text);
    
        // trim
        $text = trim($text, '-');
    
        // remove unwanted characters
        $text = preg_replace('~[^-\w.]+~', '', $text);
    
        $text = strtolower($text);
    
        return $text;
    }
    
        11
  •  2
  •   Serty Oan    15 年前

    你可以看看 Normalizer::normalize() , see here . 它只需要为PHP加载intl模块

        12
  •  2
  •   Bery    10 年前

    使用已经在核心中实现的东西怎么样?

    //Clean non UTF-8 characters    
    Mage::getHelper('core/string')->cleanString($str)
    

    或核心URL/URL重写方法之一。

        13
  •  2
  •   Lucas Bustamante    6 年前

    有一个很好的解决方案 here 这也涉及特殊人物。

    texto fant_stico=>texto fantastico

    function slugify( $string, $separator = '-' ) {
        $accents_regex = '~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i';
        $special_cases = array( '&' => 'and', "'" => '');
        $string = mb_strtolower( trim( $string ), 'UTF-8' );
        $string = str_replace( array_keys($special_cases), array_values( $special_cases), $string );
        $string = preg_replace( $accents_regex, '$1', htmlentities( $string, ENT_QUOTES, 'UTF-8' ) );
        $string = preg_replace("/[^a-z0-9]/u", "$separator", $string);
        $string = preg_replace("/[$separator]+/u", "$separator", $string);
        return $string;
    }
    

    作者:Natxet

        14
  •  1
  •   MTJ    10 年前

    自从 gTLDs IDNs 我不明白为什么url不应该包含andr_)s。

    只需要rawurlencode$url就可以了。大多数浏览器都在URL中显示UTF-8字符(可能不是一些古老的IE6),bit.ly/goo.gl可以用于在俄罗斯和阿拉伯语等情况下使其变短(如果需要,可以用于广告目的),或者像用户在浏览器URL上写的那样将其写在广告中。

    唯一的区别是空格“”,如果不允许,最好用“-”和“/”替换它们。

    <?php
    function slugify($url)
    {
        $url = trim($url);
    
        $url = str_replace(" ","-",$url);
        $url = str_replace("/","-slash-",$url);
        $url = rawurlencode($url);
    }
    ?>
    

    URL编码 http://www.hurtta.com/RU/%D0%9F%D1%80%D0%BE%D0%B4%D1%83%D0%BA%D1%82%D1%8B/

    URL书写 http://www.hurtta.com/RU/Продукты/

        15
  •  1
  •   daygloink    9 年前

    我是根据梅林的反应写的。不管页面上的字符编码是什么,此函数都可以工作。它也不会将单引号转换为破折号:)

    function slugify ($string) {
        $string = utf8_encode($string);
        $string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);   
        $string = preg_replace('/[^a-z0-9- ]/i', '', $string);
        $string = str_replace(' ', '-', $string);
        $string = trim($string, '-');
        $string = strtolower($string);
    
        if (empty($string)) {
            return 'n-a';
        }
    
        return $string;
    }
    
        16
  •  1
  •   Starboy    8 年前

    在本地主机上一切正常,但在服务器上它帮助我在“mb-strtolower”上设置了“locale”和“utf-8”。

    <?
    setlocale( LC_ALL, "en_US.UTF8" );
    function slug( $string )
    {
        $string = iconv( "utf-8", "us-ascii//translit//ignore", $string ); // transliterate
        $string = str_replace( "'", "", $string );
        $string = preg_replace( "~[^\pL\d]+~u", "-", $string ); // replace non letter or non digits by "-"
        $string = preg_replace( "~[^-\w]+~", "", $string ); // remove unwanted characters
        $string = preg_replace( "~-+~", "-", $string ); // remove duplicate "-"
        $string = trim( $string, "-" ); // trim "-"
        $string = trim( $string ); // trim
        $string = mb_strtolower( $string, "utf-8" ); // lowercase
        $string = urlencode( $string ); // safe
        return $string;
    };
    ?>
    
        17
  •  1
  •   Paulo Victor    8 年前

    我认为最优雅的方式是使用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

        18
  •  1
  •   Félix O.    6 年前

    我不知道该用哪一个,所以我在phptester.net上做了一个快速的长凳。

    <?php
    
    // First test
    // https://stackoverflow.com/a/42740874/10232729
    function slugify(STRING $string, STRING $separator = '-'){
    
        $accents_regex = '~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i';
        $special_cases = [ '&' => 'and', "'" => ''];
        $string = mb_strtolower( trim( $string ), 'UTF-8' );
        $string = str_replace( array_keys($special_cases), array_values( $special_cases), $string );
        $string = preg_replace( $accents_regex, '$1', htmlentities( $string, ENT_QUOTES, 'UTF-8' ) );
        $string = preg_replace('/[^a-z0-9]/u', $separator, $string);
    
        return preg_replace('/['.$separator.']+/u', $separator, $string);
    }
    
    // Second test
    // https://stackoverflow.com/a/13331948/10232729
    function slug(STRING $string, STRING $separator = '-'){
    
        $string = transliterator_transliterate('Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove; Lower();', $string);
    
        return str_replace(' ', $separator, $string);;
    }
    
    // Third test - My choice
    // https://stackoverflow.com/a/38066136/10232729
    function slugbis($text){
    
        $replace = [
            '<' => '', '>' => '', '-' => ' ', '&' => '',
            '"' => '', 'À' => 'A', 'Á' => 'A', 'Â' => 'A', 'Ã' => 'A', 'Ä'=> 'Ae',
            'Ä' => 'A', 'Å' => 'A', 'Ā' => 'A', 'Ą' => 'A', 'Ă' => 'A', 'Æ' => 'Ae',
            'Ç' => 'C', 'Ć' => 'C', 'Č' => 'C', 'Ĉ' => 'C', 'Ċ' => 'C', 'Ď' => 'D', 'Đ' => 'D',
            'Ð' => 'D', 'È' => 'E', 'É' => 'E', 'Ê' => 'E', 'Ë' => 'E', 'Ē' => 'E',
            'Ę' => 'E', 'Ě' => 'E', 'Ĕ' => 'E', 'Ė' => 'E', 'Ĝ' => 'G', 'Ğ' => 'G',
            'Ġ' => 'G', 'Ģ' => 'G', 'Ĥ' => 'H', 'Ħ' => 'H', 'Ì' => 'I', 'Í' => 'I',
            'Î' => 'I', 'Ï' => 'I', 'Ī' => 'I', 'Ĩ' => 'I', 'Ĭ' => 'I', 'Į' => 'I',
            'İ' => 'I', 'IJ' => 'IJ', 'Ĵ' => 'J', 'Ķ' => 'K', 'Ł' => 'K', 'Ľ' => 'K',
            'Ĺ' => 'K', 'Ļ' => 'K', 'Ŀ' => 'K', 'Ñ' => 'N', 'Ń' => 'N', 'Ň' => 'N',
            'Ņ' => 'N', 'Ŋ' => 'N', 'Ò' => 'O', 'Ó' => 'O', 'Ô' => 'O', 'Õ' => 'O',
            'Ö' => 'Oe', 'Ö' => 'Oe', 'Ø' => 'O', 'Ō' => 'O', 'Ő' => 'O', 'Ŏ' => 'O',
            'Œ' => 'OE', 'Ŕ' => 'R', 'Ř' => 'R', 'Ŗ' => 'R', 'Ś' => 'S', 'Š' => 'S',
            'Ş' => 'S', 'Ŝ' => 'S', 'Ș' => 'S', 'Ť' => 'T', 'Ţ' => 'T', 'Ŧ' => 'T',
            'Ț' => 'T', 'Ù' => 'U', 'Ú' => 'U', 'Û' => 'U', 'Ü' => 'Ue', 'Ū' => 'U',
            'Ü' => 'Ue', 'Ů' => 'U', 'Ű' => 'U', 'Ŭ' => 'U', 'Ũ' => 'U', 'Ų' => 'U',
            'Ŵ' => 'W', 'Ý' => 'Y', 'Ŷ' => 'Y', 'Ÿ' => 'Y', 'Ź' => 'Z', 'Ž' => 'Z',
            'Ż' => 'Z', 'Þ' => 'T', 'à' => 'a', 'á' => 'a', 'â' => 'a', 'ã' => 'a',
            'ä' => 'ae', 'ä' => 'ae', 'å' => 'a', 'ā' => 'a', 'ą' => 'a', 'ă' => 'a',
            'æ' => 'ae', 'ç' => 'c', 'ć' => 'c', 'č' => 'c', 'ĉ' => 'c', 'ċ' => 'c',
            'ď' => 'd', 'đ' => 'd', 'ð' => 'd', 'è' => 'e', 'é' => 'e', 'ê' => 'e',
            'ë' => 'e', 'ē' => 'e', 'ę' => 'e', 'ě' => 'e', 'ĕ' => 'e', 'ė' => 'e',
            'ƒ' => 'f', 'ĝ' => 'g', 'ğ' => 'g', 'ġ' => 'g', 'ģ' => 'g', 'ĥ' => 'h',
            'ħ' => 'h', 'ì' => 'i', 'í' => 'i', 'î' => 'i', 'ï' => 'i', 'ī' => 'i',
            'ĩ' => 'i', 'ĭ' => 'i', 'į' => 'i', 'ı' => 'i', 'ij' => 'ij', 'ĵ' => 'j',
            'ķ' => 'k', 'ĸ' => 'k', 'ł' => 'l', 'ľ' => 'l', 'ĺ' => 'l', 'ļ' => 'l',
            'ŀ' => 'l', 'ñ' => 'n', 'ń' => 'n', 'ň' => 'n', 'ņ' => 'n', 'ʼn' => 'n',
            'ŋ' => 'n', 'ò' => 'o', 'ó' => 'o', 'ô' => 'o', 'õ' => 'o', 'ö' => 'oe',
            'ö' => 'oe', 'ø' => 'o', 'ō' => 'o', 'ő' => 'o', 'ŏ' => 'o', 'œ' => 'oe',
            'ŕ' => 'r', 'ř' => 'r', 'ŗ' => 'r', 'š' => 's', 'ù' => 'u', 'ú' => 'u',
            'û' => 'u', 'ü' => 'ue', 'ū' => 'u', 'ü' => 'ue', 'ů' => 'u', 'ű' => 'u',
            'ŭ' => 'u', 'ũ' => 'u', 'ų' => 'u', 'ŵ' => 'w', 'ý' => 'y', 'ÿ' => 'y',
            'ŷ' => 'y', 'ž' => 'z', 'ż' => 'z', 'ź' => 'z', 'þ' => 't', 'ß' => 'ss',
            'ſ' => 'ss', 'ый' => 'iy', 'А' => 'A', 'Б' => 'B', 'В' => 'V', 'Г' => 'G',
            'Д' => 'D', 'Е' => 'E', 'Ё' => 'YO', 'Ж' => 'ZH', 'З' => 'Z', 'И' => 'I',
            'Й' => 'Y', 'К' => 'K', 'Л' => 'L', 'М' => 'M', 'Н' => 'N', 'О' => 'O',
            'П' => 'P', 'Р' => 'R', 'С' => 'S', 'Т' => 'T', 'У' => 'U', 'Ф' => 'F',
            'Х' => 'H', 'Ц' => 'C', 'Ч' => 'CH', 'Ш' => 'SH', 'Щ' => 'SCH', 'Ъ' => '',
            'Ы' => 'Y', 'Ь' => '', 'Э' => 'E', 'Ю' => 'YU', 'Я' => 'YA', 'а' => 'a',
            'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd', 'е' => 'e', 'ё' => 'yo',
            'ж' => 'zh', 'з' => 'z', 'и' => 'i', 'й' => 'y', 'к' => 'k', 'л' => 'l',
            'м' => 'm', 'н' => 'n', 'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's',
            'т' => 't', 'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c', 'ч' => 'ch',
            'ш' => 'sh', 'щ' => 'sch', 'ъ' => '', 'ы' => 'y', 'ь' => '', 'э' => 'e',
            'ю' => 'yu', 'я' => 'ya'
        ];
    
        // make a human readable string
        $text = strtr($text, $replace);
    
        // replace non letter or digits by -
        $text = preg_replace('~[^\pL\d.]+~u', '-', $text);
    
        // trim
        $text = trim($text, '-');
    
        // remove unwanted characters
        $text = preg_replace('~[^-\w.]+~', '', $text);
    
        return strtolower($text);
    }
    
    // Fourth test
    // https://stackoverflow.com/a/2955521/10232729
    function slugagain($string){
    
        $table = [
            'Š'=>'S', 'š'=>'s', 'Đ'=>'Dj', 'đ'=>'dj', 'Ž'=>'Z', 'ž'=>'z', 'Č'=>'C', 'č'=>'c', 'Ć'=>'C', 'ć'=>'c',
            'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A', 'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E',
            'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I', 'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O',
            'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U', 'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss',
            'à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a', 'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e',
            'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i', 'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o',
            'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u', 'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b',
            'ÿ'=>'y', 'Ŕ'=>'R', 'ŕ'=>'r', ' '=>'-'
        ];
    
        return strtr($string, $table);
    }
    
    // Fifth test
    // https://stackoverflow.com/a/27396804/10232729
    function slugifybis($url){
        $url = trim($url);
    
        $url = str_replace(' ', '-', $url);
        $url = str_replace('/', '-slash-', $url);
    
        return rawurlencode($url);
    }
    
    // Sixth and last test
    // https://stackoverflow.com/a/39442034/10232729
    setlocale( LC_ALL, "en_US.UTF8" );  
    function slugifyagain($string){
    
        $string = iconv('utf-8', 'us-ascii//translit//ignore', $string); // transliterate
        $string = str_replace("'", '', $string);
        $string = preg_replace('~[^\pL\d]+~u', '-', $string); // replace non letter or non digits by "-"
        $string = preg_replace('~[^-\w]+~', '', $string); // remove unwanted characters
        $string = preg_replace('~-+~', '-', $string); // remove duplicate "-"
        $string = trim($string, '-'); // trim "-"
        $string = trim($string); // trim
        $string = mb_strtolower($string, 'utf-8'); // lowercase
    
        return urlencode($string); // safe;
    };
    
    $string = $newString = "¿ Àñdréß l'affreux ğarçon & nøël en forêt !";
    
    $max = 10000;
    
    echo '<pre>';
    echo 'Beginning :';
    echo '<br />';
    echo '<br />';    
    echo '> Slugging '.$max.' iterations of following :';
    echo '<br />';
    echo '>> ' . $string;
    echo '<br />';  
    echo '<br />';
    echo 'Output results :';
    echo '<br />';
    echo '<br />';  
    
    $start = microtime(true);
    
    for($i = 0 ; $i < $max ; $i++){
    
        $newString = slugify($string);
    }
    
    $time = (microtime(true) - $start) * 1000;
    
    echo '> First test passed in **' . round($time, 2) . 'ms**';
    echo '<br />';  
    echo '>> Result : ' . $newString;
    echo '<br />';
    echo '<br />';
    
    $start = microtime(true);
    
    for($i = 0 ; $i < $max ; $i++){
    
        $newString = slug($string);
    }
    
    $time = (microtime(true) - $start) * 1000;
    
    echo '> Second test passed in **' . round($time, 2) . 'ms**';
    echo '<br />';
    echo '>> Result : ' . $newString;
    echo '<br />';
    echo '<br />';
    
    $start = microtime(true);
    
    for($i = 0 ; $i < $max ; $i++){
    
        $newString = slugbis($string);
    }
    
    $time = (microtime(true) - $start) * 1000;
    
    echo '> Third test passed in **' . round($time, 2) . 'ms**';
    echo '<br />';
    echo '>> Result : ' . $newString;
    echo '<br />';
    echo '<br />';
    
    $start = microtime(true);
    
    for($i = 0 ; $i < $max ; $i++){
    
        $newString = slugagain($string);
    }
    
    $time = (microtime(true) - $start) * 1000;
    
    echo '> Fourth test passed in **' . round($time, 2) . 'ms**';
    echo '<br />';
    echo '>> Result : ' . $newString;
    echo '<br />';
    echo '<br />';
    
    $start = microtime(true);
    
    for($i = 0 ; $i < $max ; $i++){
    
        $newString = slugifybis($string);
    }
    
    $time = (microtime(true) - $start) * 1000;
    
    echo '> Fifth test passed in **' . round($time, 2) . 'ms**';
    echo '<br />';
    echo '>> Result : ' . $newString;
    echo '<br />';
    echo '<br />';
    
    $start = microtime(true);
    
    for($i = 0 ; $i < $max ; $i++){
    
        $newString = slugifyagain($string);
    }
    
    $time = (microtime(true) - $start) * 1000;
    
    echo '> Sixth test passed in **' . round($time, 2) . 'ms**';
    echo '<br />';
    echo '>> Result : ' . $newString;
    echo '</pre>';
    

    开始:

    重击10000次以下迭代:

    _?__?Dr___L'Affreux_ar_ on&n_ l en for_?t!

    输出结果:

    第一次测试通过 120 .78毫秒

    结果:艾克斯特·安德雷斯·拉弗鲁克斯·阿肯和诺埃尔·恩弗雷特-

    第二次测试通过 38 83.82MS

    结果:Andre_-Laffreux Garcon-N_ El en Foret-

    第三次测试通过 53.83MS

    结果:Andress-L-Affreux-Garcon-Noel-en-Foret

    第四次考试通过 18.93MS

    结果:安德烈斯-阿弗鲁克斯-阿肯-&-noel en foret-!

    第五次测试通过 4.45毫秒

    结果:%c2%bf-%c3%80%c3%b1dr%c3%a9%c3%9f-l%27afbreux-%c4%9far%c3%a7on-%26-n%c3%b8%c3%abl en for%c3%aat-%21

    第六次考试通过 112.42MS

    结果:Andress-Laffreux-Garcon-N-el-en-Foret

    需要进一步的测试。

    编辑:更少迭代测试

    开始:

    重击100次以下迭代:

    _?__ Dr_)L'Affreux_ ar_ on&n_ l en for_-t!

    输出结果:

    第一次测试通过 1.72MS

    结果:艾克斯特·安德雷斯·拉弗鲁克斯·阿肯和诺埃尔·恩弗雷特-

    第二次测试通过 45.59MS

    结果:Andre_-Laffreux Garcon-N_ El en Foret-

    第三次测试通过 0.91MS

    结果:Andress-L-Affreux-Garcon-Noel-en-Foret

    第四次考试通过 0.3MS

    结果:安德烈斯-阿弗鲁克斯-阿肯-&-noel en foret-!

    第五次测试通过 0.14MS

    结果:%c2%bf-%c3%80%c3%b1dr%c3%a9%c3%9f-l%27afbreux-%c4%9far%c3%a7on-%26-n%c3%b8%c3%abl en for%c3%aat-%21

    第六次考试通过 1.4毫秒

    结果:Andress-Laffreux-Garcon-N-el-en-Foret

        19
  •  0
  •   Ima    9 年前

    我有一个在西班牙网站上工作的代码。请看我博客里的代码

    Function to generate clean url slugs from string with duplication check