Skip to content

Instantly share code, notes, and snippets.

@jemekite
Created October 9, 2015 17:56
Show Gist options
  • Save jemekite/0774eac1cf816780ada9 to your computer and use it in GitHub Desktop.
Save jemekite/0774eac1cf816780ada9 to your computer and use it in GitHub Desktop.
Clean URL
function cleanURL($string)
{
$url = str_replace("'", '', $string);
$url = str_replace('%20', ' ', $url);
$url = preg_replace('~[^\\pL0-9_]+~u', '-', $url); // substitutes anything but letters, numbers and '_' with separator
$url = trim($url, "-");
$url = iconv("utf-8", "us-ascii//TRANSLIT", $url); // you may opt for your own custom character map for encoding.
$url = strtolower($url);
$url = preg_replace('~[^-a-z0-9_]+~', '', $url); // keep only letters, numbers, '_' and separator
return $url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment