R
R
Ravshan Enikeev2018-03-19 22:07:01
PHP
Ravshan Enikeev, 2018-03-19 22:07:01

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:
5ab00a0e43b3b382958305.jpeg

Answer the question

In order to leave comments, you need to log in

3 answer(s)
S
Sergey Epifanov, 2018-03-19
@kacheleff

does it fit like this?
if anything, I took the solution from here and redid it a bit

K
ksnk, 2018-03-19
@ksnk

$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);

enters "Balance_on". Perhaps this will be of some help

S
Stalker_RED, 2018-03-20
@Stalker_RED

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 question

Ask a Question

731 491 924 answers to any question