Skip to content

Instantly share code, notes, and snippets.

@sbrl
Created March 5, 2018 21:31
Show Gist options
  • Select an option

  • Save sbrl/09ba8e2215f0c1e4af8be46b205e0866 to your computer and use it in GitHub Desktop.

Select an option

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!
<?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