Skip to content

Instantly share code, notes, and snippets.

@gravataLonga
Last active April 9, 2016 14:38
Show Gist options
  • Save gravataLonga/7309461 to your computer and use it in GitHub Desktop.
Save gravataLonga/7309461 to your computer and use it in GitHub Desktop.
Clean URL
<?php
function cleanForShortURL($toClean) {
$charactersToRemove = array(
'Š'=>'S', 'š'=>'s', 'Ð'=>'Dj','Ž'=>'Z', 'ž'=>'z', 'À'=>'A', 'Á'=>'A', 'Â'=>'A', 'Ã'=>'A', 'Ä'=>'A',
'Å'=>'A', 'Æ'=>'A', 'Ç'=>'C', 'È'=>'E', 'É'=>'E', 'Ê'=>'E', 'Ë'=>'E', 'Ì'=>'I', 'Í'=>'I', 'Î'=>'I',
'Ï'=>'I', 'Ñ'=>'N', 'Ò'=>'O', 'Ó'=>'O', 'Ô'=>'O', 'Õ'=>'O', 'Ö'=>'O', 'Ø'=>'O', 'Ù'=>'U', 'Ú'=>'U',
'Û'=>'U', 'Ü'=>'U', 'Ý'=>'Y', 'Þ'=>'B', 'ß'=>'Ss','à'=>'a', 'á'=>'a', 'â'=>'a', 'ã'=>'a', 'ä'=>'a',
'å'=>'a', 'æ'=>'a', 'ç'=>'c', 'è'=>'e', 'é'=>'e', 'ê'=>'e', 'ë'=>'e', 'ì'=>'i', 'í'=>'i', 'î'=>'i',
'ï'=>'i', 'ð'=>'o', 'ñ'=>'n', 'ò'=>'o', 'ó'=>'o', 'ô'=>'o', 'õ'=>'o', 'ö'=>'o', 'ø'=>'o', 'ù'=>'u',
'ú'=>'u', 'û'=>'u', 'ý'=>'y', 'ý'=>'y', 'þ'=>'b', 'ÿ'=>'y', 'ƒ'=>'f'
);
$toClean = strtr($toClean, $charactersToRemove);
$toClean = str_replace('&quot;', '', $toClean);
$toClean = str_replace('&', '-and-', $toClean);
$toClean = trim(preg_replace('/[^\w\d_ -]/si', '', $toClean));//remove all illegal chars
$toClean = str_replace(' ', '-', $toClean);
$toClean = str_replace('--', '-', $toClean);
$toClean = strtolower($toClean);
$toClean = str_replace('--', '-', $toClean);
$toClean = strtr($toClean, $charactersToRemove);
return $toClean;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment