Skip to content

Instantly share code, notes, and snippets.

@csilverman
Created February 5, 2020 21:35
Show Gist options
  • Save csilverman/adf7346cc2b89ec8a85c302a7b9d6fb8 to your computer and use it in GitHub Desktop.
Save csilverman/adf7346cc2b89ec8a85c302a7b9d6fb8 to your computer and use it in GitHub Desktop.
function slugify($string){
// /[^A-Za-z0-9-]+/
// $final_string = strtolower(trim(preg_replace('/[^A-Za-z0-9-]+/', '-', $string), '-'));
// Get rid of multiple spaces
$final_string = str_ireplace(' ', ' ', $string);
// target all alphanumerics
// https://stackoverflow.com/a/17947853/6784304
$replace_pattern = '/\W|_/';
$final_string = strtolower(trim(preg_replace($replace_pattern, '-', $final_string), '-'));
// get rid of multiple hyphens because I'm OCD about this sort of thing
// https://stackoverflow.com/a/29865564/6784304
$final_string = preg_replace('/-+/', '-', $final_string);
return $final_string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment