我想有一个函数从Unicode字符串创建鼻涕虫,例如gen_slug('Andrés Cortez')应该返回andres-cortez。我该怎么做呢?


当前回答

不确定它适用于每一种情况,但我从Laravel Str类中采用了slug方法,并添加了iconv('utf-8', 'us-ascii//TRANSLIT', $title)的东西来处理口音,而不需要使用voku/便携式ascii,这似乎对我的用例工作得很好:

    public static function slug($title, $separator = '-')
    {
        $title = iconv('utf-8', 'us-ascii//TRANSLIT', $title);
        $flip = $separator === '-' ? '_' : '-';
        $title = preg_replace('!['.preg_quote($flip).']+!u', $separator, $title);
        // Replace @ with the word 'at'
        $title = str_replace('@', $separator.'at'.$separator, $title);
        // Remove all characters that are not the separator, letters, numbers, or whitespace.
        $title = preg_replace('![^'.preg_quote($separator).'\pL\pN\s]+!u', '', mb_strtolower($title, 'UTF-8'));
        // Replace all separator characters and whitespace by a single separator
        $title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title);

        return trim($title, $separator);
    }

其他回答

更新

由于这个答案引起了一些关注,我在这里添加了一些解释。

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

只有当您确信输入字符串不会包含unicode字符时才使用此解决方案,您可能希望这些字符成为output/slug的一部分。

如。“नारीशक्ति”将成为 "----------" ( 连字符)而不是“नारी——शक्ति”(有效的URL蛞蝓)。

回答

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

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

对我来说,这个变体是完美的,它也改变&和。下面是代码:

function dSlug($string) {
    return strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1',htmlentities(preg_replace('/[&]/', ' and ', $title), ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
}`

我根据梅林的回答写了这篇文章。无论页面上的字符编码如何,此函数都将正常工作。它也不会把单引号变成破折号:)

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;
}

这可能也是一种方法。灵感来自这些链接专家交流和alinalexander

function slugifier($txt){

   /* Get rid of accented characters */
   $search = explode(",","ç,æ,œ,á,é,í,ó,ú,à,è,ì,ò,ù,ä,ë,ï,ö,ü,ÿ,â,ê,î,ô,û,å,e,i,ø,u");
   $replace = explode(",","c,ae,oe,a,e,i,o,u,a,e,i,o,u,a,e,i,o,u,y,a,e,i,o,u,a,e,i,o,u");
   $txt = str_replace($search, $replace, $txt);

   /* Lowercase all the characters */
   $txt = strtolower($txt);

   /* Avoid whitespace at the beginning and the ending */
   $txt = trim($txt);

   /* Replace all the characters that are not in a-z or 0-9 by a hyphen */
   $txt = preg_replace("/[^a-z0-9]/", "-", $txt);
   /* Remove hyphen anywhere it's more than one */
   $txt = preg_replace("/[\-]+/", '-', $txt);
   return $txt;   
}