Skip to content

Instantly share code, notes, and snippets.

@mokoshalb
Forked from tlongren/example.php
Last active October 30, 2017 09:14
Show Gist options
  • Save mokoshalb/8d3ec220cd7858de5b6c8f6808fd0ad8 to your computer and use it in GitHub Desktop.
Save mokoshalb/8d3ec220cd7858de5b6c8f6808fd0ad8 to your computer and use it in GitHub Desktop.
PHP Clean URL (SLUG) Generator
<?php
setlocale(LC_ALL, 'en_US.UTF8');
function slugit($str, $replace=array(), $delimiter='-') {
if ( !empty($replace) ) {
$str = str_replace((array)$replace, ' ', $str);
}
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $str);
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower(trim($clean, '-'));
$clean = preg_replace("/[\/_|+ -]+/", $delimiter, $clean);
return $clean;
}
?>
<?php
$slug = slugit("thank you for visiting");
echo $slug;
// returns: thank-you-for-visiting
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment