Answer the question
In order to leave comments, you need to log in
PHP: how to write a script to rename crooked filenames?
Hello.
The situation is this: Now hosting files are with names like: #U0411#U0430#U043b#U0430#U043d#U0441_#U043d#U0430_01012017.pdf
And it should be: Balance_on_01012017.pdf (some kind of encoding glitch and file names went).
You need to write a script that would change the file names to the correct ones PER CHARACTER.
Colleagues seem to say that this can be done in two ways:
1) Through a replacement / substitution table
2) Some Unicode function (or something like that) that fixes a buggy encoding.
You can suggest other ways if you know.
In general, at the beginning I wrote a working solution that replaced the value "#U0411#U0430#U043b#U0430#U043d#U0441_#U043d#U0430_" with "Balance_to_", but it was banned, they say there is not only "Balance_on_DDMMYYYY", but also a bunch of others variations and the pier need to be changed character by character:
Answer the question
In order to leave comments, you need to log in
does it fit like this?
if anything, I took the solution from here and redid it a bit
$x='#U0411#U0430#U043b#U0430#U043d#U0441_#U043d#U0430';
echo preg_replace_callback('/[#_]u([0-9a-fA-F]{4})/i', function ($match) {
return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
}, $x);
There was already the same PHP question today: renaming part of a filename in a directory?
Is this some kind of interview, or are you making cartoons?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question