Skip to content

Instantly share code, notes, and snippets.

@alexdumont
Created May 12, 2015 08:08
Show Gist options
  • Save alexdumont/addedecf40839516ba6b to your computer and use it in GitHub Desktop.
Save alexdumont/addedecf40839516ba6b to your computer and use it in GitHub Desktop.
Function which create a slug from string
<?php
function slug($str, $delimiter='-', $charset='utf-8') {
$str = preg_replace("`\[.*\]`U","",$str);
$str = preg_replace('`&(amp;)?#?[a-z0-9]+;`i','-',$str);
$str = htmlentities($str, ENT_NOQUOTES, $charset);
$str = preg_replace( "`&([a-z])(acute|uml|circ|grave|ring|cedil|slash|tilde|caron|lig);`i","\\1", $str );
$str = preg_replace( array("`[^a-z0-9]`i","`[-]+`") , "-", $str);
$str = ( $str == "" ) ? $type : strtolower(trim($str, '-'));
$str = strtolower($str);
return $str;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment