Hi,

I was searching on google for a good inflector slug in As3 to match easily the urls of my labs section and my blogs made with CakePhp. I found this good class : Inflector AS3. Unfortunately the slug function didn't work correctly. so I modified this version to obtain a slug function that provides the same result as the inflector Helpers from CakePhp 1.2.

here is the modified version of it :

code :

// It'isn't the best way to write it but it's more readable like this :
 
public function slug (word:String,replacement:String="_"):String{
 
        var aPattern:RegExp = /[àáåâ]/gi; 
        var ePattern:RegExp = /[èéêẽë]/gi; 
	var iPattern:RegExp = /[ìíî]/gi; 
	var oPattern:RegExp = /[òóôø]/gi;
	var uPattern:RegExp = /[ùúůû]/gi;
	var cPattern:RegExp = /[ç]/gi;
	var nPattern:RegExp = /[ñ]/gi;
	var aePattern:RegExp = /[äæ]/gi;
	var oePattern:RegExp = /[ö]/gi;
	var uePattern:RegExp = /[ü]/gi;
	var AePattern:RegExp = /[Ä]/gi;
	var UePattern:RegExp = /[Ü]/gi;
	var OePattern:RegExp = /[Ö]/gi;
	var ssPattern:RegExp = /[ß]/gi;
 
	word = word.replace(aPattern, 'a');
	word = word.replace(ePattern, 'e');
	word = word.replace(iPattern, 'i');
	word = word.replace(oPattern, 'o');
	word = word.replace(uPattern, 'u');
	word = word.replace(cPattern, 'c');
	word = word.replace(nPattern, 'n');
	word = word.replace(aePattern, 'ae');
	word = word.replace(oePattern, 'oe');
	word = word.replace(uePattern, 'ue');
	word = word.replace(AePattern, 'Ae');
	word = word.replace(UePattern, 'Ue');
	word = word.replace(OePattern, 'Oe');
	word = word.replace(ssPattern, 'ss');
 
	var startPattern:RegExp = /[^\w]/gi;
	word = word.replace(startPattern, replacement);
	return word;
}

Sample :

imagine a french title from an article like "Découvrez un univers inattendu au coeur de la forêt"

  • With CakePhp :
echo Inflector::slug('Découvrez un univers inattendu au coeur de la forêt','-');
//OUTPUT => Decouvrez-un-univers-inattendu-au-coeur-de-la-foret
  • In ActionScript 3.0
trace(slug("Découvrez un univers inattendu au coeur de la forêt","-");
//OUTPUT => Decouvrez-un-univers-inattendu-au-coeur-de-la-foret

Enjoy