Skip to content

Instantly share code, notes, and snippets.

@jpalala
Forked from anonymous/gist:2912227
Last active September 26, 2022 05:32
Show Gist options
  • Save jpalala/4a67601f83c9c571d26c to your computer and use it in GitHub Desktop.
Save jpalala/4a67601f83c9c571d26c to your computer and use it in GitHub Desktop.
Slug URL generator for seo (removes spaces hyphens, and other 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);
}
@jpalala
Copy link
Author

jpalala commented Nov 5, 2014

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment