Skip to content

Instantly share code, notes, and snippets.

@fhferreira
Created January 11, 2020 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fhferreira/2162a213fb2a9b2a6430de153a3bd1f2 to your computer and use it in GitHub Desktop.
Save fhferreira/2162a213fb2a9b2a6430de153a3bd1f2 to your computer and use it in GitHub Desktop.
Slug php
<?php
function slugify($text)
{
// replace non letter or digits by -
$text = preg_replace('~[^\pL\d]+~u', '-', $text);
// transliterate
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
// trim
$text = trim($text, '-');
// remove duplicate -
$text = preg_replace('~-+~', '-', $text);
// lowercase
$text = strtolower($text);
if (empty($text)) {
return 'n-a';
}
return $text;
}
//using
echo slugify('Pioneering Change Conference: Night of Worship');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment