Daily Archives: 28 March 2013

dasherize

i had to write a “dasherize” function to generate SEO friendly url and so i found the power of unicode normalization…

#!/usr/bin/perl

use warnings;
use strict;
use utf8;
use Unicode::Normalize;

my $string = 'accentate «àèìòù» e “euro” (¤ç) {¹²³} [prova]';
my $length = 200;

binmode(STDOUT, ":utf8");

print "$string\n";

$string = NFKD($string);
$string =~ s/\pM//og;

$string =~ s/[^a-z0-9]+/-/gi;
$string =~ s/^(.{1,$length}).*/\L$1/;
$string =~ s/-[^-]*?$//;

print "$string\n";

and here is the output:

fabio@gnu64:~/tmp$ ./dasherize
accentate «àèìòù» e “euro” (¤ç) {¹²³} [prova]
accentate-aeiou-e-euro-c-123-prova