Skip to content

Instantly share code, notes, and snippets.

Created June 11, 2012 19:44
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save anonymous/2912227 to your computer and use it in GitHub Desktop.
Save anonymous/2912227 to your computer and use it in GitHub Desktop.
php slugify string
<?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);
}
@tupakapoor
Copy link

return preg_replace('/\-{2,}', '-', $str);
should be
return preg_replace('/\-{2,}/', '-', $str);
with the added slash at the end of the regex

@tmtinteractive
Copy link

tmtinteractive commented Jun 24, 2018

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);
}

@junieldantas
Copy link

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