Created
March 5, 2018 21:31
-
-
Save sbrl/09ba8e2215f0c1e4af8be46b205e0866 to your computer and use it in GitHub Desktop.
[slugify] A simple slugifier in PHP. Makes any string safe for inclusion in filenames / paths, urls, and more!
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
| <?php | |
| /** | |
| * Normalises and removes any unknown characters from a string, making it safe | |
| * for use as a filename. | |
| * @param string $string The string to slugificate. | |
| * @param integer $maxlength The maximum desired length of the resultant string. | |
| * @return string The slugified string. | |
| */ | |
| function slugify($string, $maxlength = 50) { | |
| return substr(preg_replace([ | |
| '/\s+/', | |
| '/[^a-z0-9\-_]/' | |
| ], [ | |
| '-', | |
| '' | |
| ], | |
| strtolower(trim($string)) | |
| ), 0, $maxlength); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment