Skip to content

Instantly share code, notes, and snippets.

@jmillerdesign
Created May 10, 2014 05:07
Show Gist options
  • Select an option

  • Save jmillerdesign/b1149f417dde5f6c9b62 to your computer and use it in GitHub Desktop.

Select an option

Save jmillerdesign/b1149f417dde5f6c9b62 to your computer and use it in GitHub Desktop.
Slugify a string
<?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