如何在 slugify 函数中将“:”替换为“/”


How can I replace ":" with "/" in slugify function?

我有一个使文本弹奏的函数,它运行良好,除了我需要将":"替换为"/"。目前,它将所有非字母或数字替换为"-"。在这里:

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

我只做了一些修改。我提供了一组搜索/替换数组,让我们用-替换大多数内容,但用/替换:

$search = array( '~[^''pL'd:]+~u', '~:~' );
$replace = array( '-', '/' );
$text = preg_replace( $search, $replace, $text);

后来,最后一个preg_replace用空字符串替换了我们的/。所以我允许在字符类中使用正斜杠。

$text = preg_replace('~[^-'w'/]+~', '', $text);

输出以下内容:

// antiques/antiquities
echo slugify( "Antiques:Antiquities" );