Str.php
public static function slug(string $str, string $delimiter = '-') : string
    {
        $str = static::toAscii($str);

        // Remove all characters that are neither alphanumeric, nor the separator nor a whitespace.
        $str = preg_replace('![^'.preg_quote($delimiter).'\pL\pN\s]+!u', '', mb_strtolower($str));

        // Standardize the separator.
        $flip = $delimiter == '-' ? '_' : '-';
        $str = preg_replace('!['.preg_quote($flip).']+!u', $delimiter, $str);

        // Replace all separator characters and whitespace by a single separator.
        $str = preg_replace('!['.preg_quote($delimiter).'\s]+!u', $delimiter, $str);

        return trim($str, $delimiter);
    }
public static function slug(string $str, string $delimiter = '-') : string
    {
        $str = static::toAscii($str);

        // Remove all characters that are neither alphanumeric, nor the separator nor a whitespace.
        $str = preg_replace('![^'.preg_quote($delimiter).'\pL\pN\s]+!u', '', mb_strtolower($str));

        // Standardize the separator.
        $flip = $delimiter == '-' ? '_' : '-';
        $str = preg_replace('!['.preg_quote($flip).']+!u', $delimiter, $str);

        // Replace all separator characters and whitespace by a single separator.
        $str = preg_replace('!['.preg_quote($delimiter).'\s]+!u', $delimiter, $str);

        return trim($str, $delimiter);
    }
public static function slug ( string $ str , string $ delimiter = '-' ) : string { $ str = static :: toAscii ( $ str ) ; // Remove all characters that are neither alphanumeric, nor the separator nor a whitespace. $ str = preg_replace ( '![^' . preg_quote ( $ delimiter ) . '\pL\pN\s]+!u' , '' , mb_strtolower ( $ str ) ) ; // Standardize the separator. $ flip = $ delimiter == '-' ? '_' : '-' ; $ str = preg_replace ( '![' . preg_quote ( $ flip ) . ']+!u' , $ delimiter , $ str ) ; // Replace all separator characters and whitespace by a single separator. $ str = preg_replace ( '![' . preg_quote ( $ delimiter ) . '\s]+!u' , $ delimiter , $ str ) ; return trim ( $ str , $ delimiter ) ; }
Generates a URL-friendly slug from the given string.

@param   string  $str        The string to slugify.
@param   string  $delimiter  The delimiter to replace non-alphanumeric characters with.
@return  string              The resulting slug.
Generates a URL - friendly slug from the given string .