How to list files with special (Norwegian) characters

I make a simple (I thought) list of file directories, for example:

$files = scandir(DOCROOT.'files'); foreach($files as $file) { echo ' <li>'.$file.PHP_EOL; } 

The problem is that the files contain Norwegian characters (Γ¦, ΓΈ, Γ₯), and for some reason they come out as question marks. Why is this?

I can apparently fix (?) This by doing this before I repeat:

 $file = mb_convert_encoding($file, 'UTF-8', 'pass'); 

But for me it makes little sense why this helps, since a pass should mean that the character encoding conversion is not performed, according to the documents ... * confused *


Here is an example: http://random.geekality.net/files/index.php

+4
source share
1 answer

It looks like the file name encoding is in ISO Latin 1, but the page is interpreted by default using UTF-8. Characters do not appear as β€œquestion marks,” but as Unicode () replacement characters. This means that a browser that attempts to interpret the byte stream as UTF-8 encounters a byte invalid in UTF-8 and instead inserts a character at that point. Switch your browser to ISO Latin 1 and see the difference (View> Encoding> ...).

So, you need to convert strings from ISO Latin 1 to UTF-8, if you indicated that your page is encoded in UTF-8 encoding. Use mb_convert_encoding($file, 'UTF-8', 'ISO-8859-1') for this.

Why this works, if you specify the encoding $from as pass , I can only guess. What you say mb_convert_encoding is to convert from pass to UTF-8. I believe that mb_convert_encoding takes the value mb_internal_encoding as the $from encoding, which is ISO Latin 1. I assume that it is equivalent to 'auto' when using $from as the parameter.

+1
source

Source: https://habr.com/ru/post/1399081/


All Articles