Skip to content

Instantly share code, notes, and snippets.

@jonathonbyrdziak
Created February 26, 2013 17:39
Show Gist options
  • Save jonathonbyrdziak/5040424 to your computer and use it in GitHub Desktop.
Save jonathonbyrdziak/5040424 to your computer and use it in GitHub Desktop.
creates a slug of any given string
<?php
/**
* Return URL-Friendly string slug
* @param string $string
* @return string
*/
function slug($string) {
//Unwanted: {UPPERCASE} ; / ? : @ & = + $ , . ! ~ * ' ( )
$string = strtolower($string);
//Strip any unwanted characters
$string = preg_replace("/[^a-z0-9_\s-]/", "", $string);
//Clean multiple dashes or whitespaces
$string = preg_replace("/[\s-]+/", " ", $string);
//Convert whitespaces and underscore to dash
$string = preg_replace("/[\s_]/", "-", $string);
return $string;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment