Skip to content

Instantly share code, notes, and snippets.

@MilkZoft
Created January 18, 2012 03:34
Show Gist options
  • Save MilkZoft/1630715 to your computer and use it in GitHub Desktop.
Save MilkZoft/1630715 to your computer and use it in GitHub Desktop.
codejobs - Slug - PHP
<?php
function slug($string) {
$characters = array("Á" => "A", "Ç" => "c", "É" => "e", "Í" => "i", "Ñ" => "n", "Ó" => "o", "Ú" => "u",
"á" => "a", "ç" => "c", "é" => "e", "í" => "i", "ñ" => "n", "ó" => "o", "ú" => "u",
"à" => "a", "è" => "e", "ì" => "i", "ò" => "o", "ù" => "u"
);
$string = strtr($string, $characters);
$string = strtolower(trim($string));
$string = preg_replace("/[^a-z0-9-]/", "-", $string);
$string = preg_replace("/-+/", "-", $string);
if(substr($string, strlen($string) - 1, strlen($string)) === "-") {
$string = substr($string, 0, strlen($string) - 1);
}
return $string;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment