One more way to slugify texts in php, without the need of the iconv() function:
public static function slugify($text) {
$text = trim($text);
$text = str_replace(".", "", $text);
$text = self::removeAccents($text);
// replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
// trim
$text = trim($text, '-');
// lowercase
$text = strtolower($text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
return $text;
}
public static function removeAccents($text) {
$text = htmlentities(utf8_decode($text));
$remove = array("&", "acute", "grave", "circ", "tilde", "uml");
$text = str_replace($remove, "", $text);
return $text;
}
Put it inside an Utils class, and use it like
$slug = Utils::slugify("nothingisclear is awesome");
reecoo 2:48 pm on March 24, 2010 Permalink
yo…aka: test comment