I’m trying to find a way to normalize a string to pass it as a filename.
I have this so far:
my_string.mb_chars.normalize(:kd).gsub(/[^\x00-\x7F]/n, '').downcase.gsub(/[^a-z]/, '_')
But first problem: the – character. I guess there is more problems with this method.
I don’t control the name, the name string can have accents, white spaces and special chars. I want to remove all of them, replace the accents with the corresponding letter (‘é’ => ‘e’) and replace the rest with the ‘_’ character.
The names are like:
- “Prélèvements – Routine”
- “Carnet de santé”
- …
I want them to be like a filename with no space/special chars:
- “prelevements_routine”
- “carnet_de_sante”
- …
Thanks for the help 🙂
Take a look at
ActiveSupport::Inflector.transliterate, it’s very useful handling this kind of chars problems. Read there:ActiveSupport::InflectorThen, you could do something like: