-
-
Save Jamiewarb/2b9c94fb46f509693bc8b91ef923ab78 to your computer and use it in GitHub Desktop.
Javascript Slugify
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
function slugify(text) { | |
return text.toString().toLowerCase() | |
.replace(/\s+/g, '-') // Replace spaces with - | |
.replace(/[^\w\-]+/g, '') // Remove all non-word chars | |
.replace(/\-\-+/g, '-') // Replace multiple - with single - | |
.replace(/^-+/, '') // Trim - from start of text | |
.replace(/-+$/, ''); // Trim - from end of text | |
} |
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
function slugify($string) { | |
$string = strtolower($string); | |
$string = preg_replace('/\s+/', '-', $string); // Replace spaces with - | |
$string = preg_replace('/[^\w\-]+/', '', $string); // Remove all non-word chars | |
$string = preg_replace('/\-\-+/', '-', $string); // Replace multiple - with single - | |
$string = preg_replace('/^-+/', '', $string); // Trim - from start of text | |
$string = preg_replace('/-+$/', '', $string); // Trim - from end of text | |
return preg_replace('/[^A-Za-z0-9-]+/', '-', $string); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment