Skip to content

Instantly share code, notes, and snippets.

@stoll
Created July 3, 2018 15:16
Show Gist options
  • Save stoll/c79bb96b068bed37f39220b30ac72a38 to your computer and use it in GitHub Desktop.
Save stoll/c79bb96b068bed37f39220b30ac72a38 to your computer and use it in GitHub Desktop.
str_slug.php
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