laravel/framework — pull request #410 by KaneCohen (2013-02-22 12:07:09+00:00)
Title: Extend Str::slug for better result consistency

Make Str::slug method return more consistent result when input string contains `separator`-like characters.

Original code:

````php
\\ returns 'this-is-myblogpost'
Str::slug('This is my_blog_post!')
````

After PQ:

````php
\\ returns 'this-is-my-blog-post'
Str::slug('This is my_blog_post!')
````

Alternative code without extra `preg_replace`, but with less readability:

````php
$title = static::ascii($title);

$flip = ($separator == '-' ? '_' : '-');

// Remove all characters that are not the separator, letters, numbers, or whitespace.
$title = preg_replace('![^'.preg_quote($separator).preg_quote($flip).'\pL\pN\s]+!u', '', mb_strtolower($title));

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

return trim($title, $separator);

````