1

I am looking for a library or helper function in PHP which could change a given text (unicode) into a valid portable filename. How can I do that. Ideally it should include converting characters like à, á, â into a and thus retaining it readability.

This post: Turn a string into a valid filename? addresses this problem to some extent but handling unicode is what I am worried about. Is there a easy straightforward way I can solve this.

Community
  • 1
  • 1
Shiv Deepak
  • 3,122
  • 5
  • 34
  • 49
  • You question title says 'PHP' but you seem to ask about Python. Can you verify and adjust as needed. Thanks. – JScoobyCed Mar 02 '12 at 02:43
  • Sorry. Typo. PHP. I got carried away with the linked post :) – Shiv Deepak Mar 02 '12 at 02:45
  • I don't think converting "á" to "a" would retain redability. Accents are part of some languages and they are important to convey the meaning of the word. Also, there's nothing invalid about an accent in a filename, you just have to remember to `utf8_decode` it before uploading it to the server, due to differences in encoding between Windows and PHP – Fran Cano May 07 '19 at 05:40

1 Answers1

3

You could try something like this:

setlocale(LC_ALL, 'en_US.utf8');
$brand_name = iconv('utf-8', 'us-ascii//TRANSLIT', $_GET['brand-name']);

From Convert Accented Characters to Non-Accented in PHP

deceze
  • 510,633
  • 85
  • 743
  • 889
Michael Robinson
  • 29,278
  • 12
  • 104
  • 130