Created
June 11, 2012 19:44
-
-
Save anonymous/2912227 to your computer and use it in GitHub Desktop.
php slugify string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function slugify($str) { | |
$search = array('Ș', 'Ț', 'ş', 'ţ', 'Ş', 'Ţ', 'ș', 'ț', 'î', 'â', 'ă', 'Î', 'Â', 'Ă', 'ë', 'Ë'); | |
$replace = array('s', 't', 's', 't', 's', 't', 's', 't', 'i', 'a', 'a', 'i', 'a', 'a', 'e', 'E'); | |
$str = str_ireplace($search, $replace, strtolower(trim($str))); | |
$str = preg_replace('/[^\w\d\-\ ]/', '', $str); | |
$str = str_replace(' ', '-', $str); | |
return preg_replace('/\-{2,}', '-', $str); | |
} |
Sugestion for remove end trace '-'
return (substr($str, -1) == '-' ? substr($str, 0, -1) : $str );
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
There's no No ending delimiter after value is returned
Corrected funtion
function slugify($str){
$search = array('Ș', 'Ț', 'ş', 'ţ', 'Ş', 'Ţ', 'ș', 'ț', 'î', 'â', 'ă', 'Î', 'Â', 'Ă', 'ë', 'Ë');
$replace = array('s', 't', 's', 't', 's', 't', 's', 't', 'i', 'a', 'a', 'i', 'a', 'a', 'e', 'E');
$str = str_ireplace($search, $replace, strtolower(trim($str)));
$str = preg_replace('/[^\w\d\-\ ]/', '', $str);
$str = str_replace(' ', '-', $str);
return preg_replace('/-{2,}/', '-', $str);
}