3

INTRODUCTION

I am working on personal project and using Symfony3. In order to upload files i am using OneUpUploaderBundle. And I am not accepting file name that consists of characters with accents, Cyrillic characters, etc. In order to do so - I am using function from CODE section

TARGET

I would like to use PHP function in CODE section in JavaScript!

CODE

// transliterate text
public function transliterateText($input_text)
{
    $input_russian = transliterator_transliterate('Russian-Latin/BGN', $input_text);
    $input_german_french = transliterator_transliterate('Any-Latin; Latin-ASCII', $input_russian);
    $input_baltic = iconv('UTF-8', 'ASCII//TRANSLIT', $input_german_french);

    $transliterated_text = preg_replace('/[^a-zA-Z_0-9\(\)\n]/', '_', $input_baltic );
    $transliterated_text = strtolower($transliterated_text);

    return $transliterated_text;
}

EXAMPLE

input: "12345 Rūķīši Проверка äöüß àâæçéèêëïîôœùûüÿ.txt"

output: "12345_rukisi_proverka_aouss_aaaeceeeeiiooeuuuy.txt"

QUESTION

I did not found many information about this problem on the Internet. May be it is not a good idea to use JavaScript for this task...

Or maybe I should create service in Symfony3, that is accessible through AJAX and returns transliterated text instead?

CONCLUSION

Please advise.

Thank You for your time and knowledge.

UPDATE

I would like to use this function in JavaScript in order to show user what the filename would look like when on the server. (File names are going to be transliterated on server anyway). At the moment I am sending (in the UploadListener) following information for each file [{'error':'none'}{'orig':'my file name.txt'}{'t13n':'my_file_name.txt'}]. I would like to send as little as possible informātion from server to browser. So if there was "translation" of the CODE I would need only to send error for each file...

Rikijs
  • 728
  • 1
  • 12
  • 48
  • 2
    I would just make an AJAX call to the PHP and have it return text to JS. – AbraCadaver Mar 24 '17 at 18:11
  • 2
    Javascript is for convenience, server side code is for protection. If you implement javascript version of this, you should not assume that the data you get is always the way you want it to be. My approach to file upload is somewhat different which you can consider if you want. When I accept file I compute sha256 hash of it and rename the file into its hash, while storing hash to file name translation in database. That way it does not matter what the file name is, and you can store all kinds of things in database. This also saves up a lot of space if you allow users to upload images. – Dimi Mar 24 '17 at 18:15
  • @Dimi Thanks, your suggestion seams like a good idea. Though I would add some random string to the sha256 hash on the off chance that 2 users are uploading same file at the same time. – Rikijs Mar 27 '17 at 17:12
  • @Rikijs if two users upload the same file, store file only once to save space. When user deletes a file, remove it from your database ONCE, check to see if any other user is referencing that file as well, if not, delete the file from harddrive. – Dimi Mar 27 '17 at 17:39
  • Would the code in [this answer](http://stackoverflow.com/a/9667817/1575353) work for your case? – Sᴀᴍ Onᴇᴌᴀ Apr 11 '17 at 20:47
  • @SamOnela - Thank You for suggestion It seems it would do the trick. – Rikijs Apr 12 '17 at 17:36
  • 1
    Possible duplicate of [Efficiently replace all accented characters in a string?](http://stackoverflow.com/questions/286921/efficiently-replace-all-accented-characters-in-a-string) – Sᴀᴍ Onᴇᴌᴀ Apr 12 '17 at 17:49

0 Answers0