public function urlize(string $string) : string
    {
        // Remove all non url friendly characters with the unaccent function
        $unaccented = $this->unaccent($string);

        if (function_exists('mb_strtolower')) {
            $lowered = mb_strtolower($unaccented);
        } else {
            $lowered = strtolower($unaccented);
        }

        $replacements = [
            '/\W/' => ' ',
            '/([A-Z]+)([A-Z][a-z])/' => '\1_\2',
            '/([a-z\d])([A-Z])/' => '\1_\2',
            '/[^A-Z^a-z^0-9^\/]+/' => '-',
        ];

        $urlized = $lowered;

        foreach ($replacements as $pattern => $replacement) {
            $replaced = preg_replace($pattern, $replacement, $urlized);

            if ($replaced === null) {
                throw new RuntimeException(sprintf(
                    'preg_replace returned null for value "%s"',
                    $urlized
                ));
            }

            $urlized = $replaced;
        }

        return trim($urlized, '-');
    }
public function urlize(string $string) : string
    {
        // Remove all non url friendly characters with the unaccent function
        $unaccented = $this->unaccent($string);

        if (function_exists('mb_strtolower')) {
            $lowered = mb_strtolower($unaccented);
        } else {
            $lowered = strtolower($unaccented);
        }

        $replacements = [
            '/\W/' => ' ',
            '/([A-Z]+)([A-Z][a-z])/' => '\1_\2',
            '/([a-z\d])([A-Z])/' => '\1_\2',
            '/[^A-Z^a-z^0-9^\/]+/' => '-',
        ];

        $urlized = $lowered;

        foreach ($replacements as $pattern => $replacement) {
            $replaced = preg_replace($pattern, $replacement, $urlized);

            if ($replaced === null) {
                throw new RuntimeException(sprintf(
                    'preg_replace returned null for value "%s"',
                    $urlized
                ));
            }

            $urlized = $replaced;
        }

        return trim($urlized, '-');
    }
public function urlize ( string $ string ) : string { // Remove all non url friendly characters with the unaccent function $ unaccented = $ this -> unaccent ( $ string ) ; if ( function_exists ( 'mb_strtolower' ) ) { $ lowered = mb_strtolower ( $ unaccented ) ; } else { $ lowered = strtolower ( $ unaccented ) ; } $ replacements = [ '/\W/' => ' ' , '/([A-Z]+)([A-Z][a-z])/' => '\1_\2' , '/([a-z\d])([A-Z])/' => '\1_\2' , '/[^A-Z^a-z^0-9^\/]+/' => '-' , ] ; $ urlized = $ lowered ; foreach ( $ replacements as $ pattern => $ replacement ) { $ replaced = preg_replace ( $ pattern , $ replacement , $ urlized ) ; if ( $ replaced === null ) { throw new RuntimeException ( sprintf ( 'preg_replace returned null for value "%s"' , $ urlized ) ) ; } $ urlized = $ replaced ; } return trim ( $ urlized , '-' ) ; }
Convert any passed string to a url friendly string.
Converts 'My first blog post' to 'my-first-blog-post'

@param  string $string String to urlize.

@return string Urlized string.
Convert any passed string to a url friendly string . Converts My first blog post to my - first - blog - post