0

I want my program to display a list of files and their content in the browser. The problem is when I use file_get_contents ($file_name) I receive a string with diamond question marks in place where Polish characters should be.

$content = file_get_contents($file_name);
echo $content;

I've tried to use:

$content = utf8_encode(file_get_contents($file_name));

But that just replaces question marks with some strange characters like: ``, ñ, ³, ¿, etc. I've tried to outsmart it by making a switch to replace them with Polish characters individualy, but that failed as the switch wasn't detecting them.

function translate($string) {
       $translated = "";
        for($i = 0; $i < strlen($string); $i++) {
         $letter = substr($string, $i, 1);
         switch($letter) {
          case '':
            $letter = 'ś';
            break;
          case 'ñ':
            $letter = 'ń';
            break;
          case '³':
            $letter = 'ł';
            break;
          case '¿':
            $letter = 'ż';
            break;
          case 'æ':
            $letter = "ć";
            break;
          case '¹':
            $letter = 'ą';
            break;
          case 'ê':
             $letter = "ę";
             break;
          case '³':
              $letter = "ć";
              break;`
           }
             $translated = $translated . $letter;
          }
           return $translated;
         }

I have to add, that all Polish characters work OK in HTML or nested PHP. Only these I get within file's content are replaced.

David Buck
  • 3,752
  • 35
  • 31
  • 35
  • Hello, does [this](https://stackoverflow.com/questions/2236668/file-get-contents-breaks-up-utf-8-characters) help? – mrodo Mar 06 '20 at 17:13
  • 1
    Does this answer your question? [file\_get\_contents() Breaks Up UTF-8 Characters](https://stackoverflow.com/questions/2236668/file-get-contents-breaks-up-utf-8-characters) – mrodo Mar 06 '20 at 17:14

0 Answers0