Created
July 3, 2018 15:16
-
-
Save stoll/c79bb96b068bed37f39220b30ac72a38 to your computer and use it in GitHub Desktop.
str_slug.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function str_slug($title, $separator = '-') { | |
// Convert all dashes/underscores into separator | |
$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)); | |
// Replace all separator characters and whitespace by a single separator | |
$title = preg_replace('!['.preg_quote($separator).'\s]+!u', $separator, $title); | |
return trim($title, $separator); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment