Created
May 10, 2014 05:07
-
-
Save jmillerdesign/b1149f417dde5f6c9b62 to your computer and use it in GitHub Desktop.
Slugify a string
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 | |
| /** | |
| * Convert a human-readable string into a slug | |
| * | |
| * 'Sample input string!' gets converted to 'sample-input-string' | |
| * | |
| * @param string Source string | |
| * @return string Slug | |
| * @author Cake Development Corporation (http://cakedc.com) | |
| */ | |
| function convertToSlug($string) { | |
| $str = mb_strtolower($string); | |
| $str = preg_replace('/\xE3\x80\x80/', ' ', $str); | |
| $str = preg_replace('[\'s ]', 's ', $str); | |
| $str = str_replace('-', ' ', $str); | |
| $str = preg_replace( '#[:\#\*"()~$^{}`@+=;,<>!&%\.\]\/\'\\\\|\[]#', "\x20", $str ); | |
| $str = str_replace('?', '', $str); | |
| $str = trim($str); | |
| $str = preg_replace('#\x20+#', '-', $str); | |
| return $str; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment