Skip to content

Instantly share code, notes, and snippets.

@HasStacey
Created June 30, 2014 23:52
Show Gist options
  • Save HasStacey/a33a9109a68eeb2b8590 to your computer and use it in GitHub Desktop.
Save HasStacey/a33a9109a68eeb2b8590 to your computer and use it in GitHub Desktop.
Create a class name based upon the value passed to the function
// Create a class name based upon the value passed through the function
// For example: if $string = "Lorem Ipsum" this will output "lorem-ipsum" as the class name
function createClass($string) {
// 1. convert to lowercase
// 2. remove special characters
// 3. Strip out multiple spaces & dashes
// 4. Convert single spaces and underscores to dashes
$className = strtolower($string); // 1
$className = preg_replace("/[^a-z0-9_\s-]/", "", $className); // 2
$className = preg_replace("/[\s-]+/", " ", $className); // 3
$className = preg_replace("/[\s_]/", "-", $className); // 4
return $className;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment