Created
June 30, 2014 23:52
-
-
Save HasStacey/a33a9109a68eeb2b8590 to your computer and use it in GitHub Desktop.
Create a class name based upon the value passed to the function
This file contains hidden or 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
// 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