Convert titles into slugs using Perl

28 Aug 2013

You want to convert a string of text, e.g. the title of a page or blog post, into a “SEO friendly” slug—using only lowercase ascii characters and hyphens—suitable for use in the url of the post.

With some hints from Stack Overflow on how to implement slugify in Perl, this is my version:

sub slugify {
  my $str = shift;

  $str = NFKD($str);        # Normalize the Unicode string
  $str = unidecode($str);   # Un-accent characters
  $str =~ tr/\000-\177//cd; # Strip non-ASCII characters (>127)
  $str =~ s/[^\w\s-]//g;    # Remove all non-word characters
  $str = lc($str);          # Lowercase
  $str =~ s/[-\s]+/-/g;     # Replace spaces and hyphens with a single hyphen
  $str =~ s/^-|-$//g;       # Trim hyphens from both ends

  return $str;
}

Just make sure that your strings are proper utf8.