Skip to content

Instantly share code, notes, and snippets.

@rafaelcalleja
Forked from dhrrgn/functions.php
Created March 17, 2022 13:03
Show Gist options
  • Select an option

  • Save rafaelcalleja/528ac4f79a1064f8b02795a906e68a55 to your computer and use it in GitHub Desktop.

Select an option

Save rafaelcalleja/528ac4f79a1064f8b02795a906e68a55 to your computer and use it in GitHub Desktop.
Slugify. Note: Requires PHP >= 5.4 and the Intl extension
<?php
function slugify($string, $toLower = true) {
$rules = "Any-Latin; Latin-ASCII; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove;";
if ($toLower) {
$rules .= ' Lower();';
}
$string = transliterator_transliterate($rules, $string);
// Remove repeating hyphens and spaces (e.g. 'foo---bar' becomes 'foo-bar')
$string = preg_replace('/[-\s]+/', '-', $string);
return trim($string, '-');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment