0

I have been working on a PHP program that at the end exports an HTML file. I am encountering an issue where sometimes French characters are not properly written to the file (it is not an issue with the webpage, I opened the file in notepad and the characters were just changed).

Here is the piece of code that I have written:

foreach($footer as $a){
echo $a;
fwrite($file, $a."\r\n");
}

The echo displays the following:

En cas de divergence entre le présent document et les documents contractuels officiels, ces derniers ont préséance.

But what is written to the file is:

En cas de divergence entre le présent document et les documents contractuels officiels, ces derniers ont préséance.

A couple lines above this in my code I export lines with other French characters and they work just fine (it is essentially exactly the same code), but here it is not.

I have tried using fputs(), changing fopen($file,'w') to fopen($file, 'wb'), adding utf_encode() (which just makes the mess that comes out look worse), etc. and nothing has fixed this issue. Any ideas?

  • Is your phpscript file set to `utf8` too? – JustOnUnderMillions Jul 27 '16 at 14:12
  • Or try something from the manual: fwrite($file, utf8_encode($myString)); – M. I. Jul 27 '16 at 14:13
  • Also be sure that the editor you are using to open the file after writing supports utf8 and is not set on latin1 – M. I. Jul 27 '16 at 14:15
  • Where does the value of variable `$a` come from? A database column? – Andy Jul 27 '16 at 14:22
  • mb_internal_encoding("UTF-8"); for PHP internal encoding. Make sure that your IDE uses utf8, else even if the content is utf8 you won't be able to see it. – M. I. Jul 27 '16 at 14:27
  • I am using Notepad++ to open the file after (it displays the same thing as when I open the HTML file in my browser). Like I said I already tried utf8_encode(), which makes the output become: "En cas de divergence entre le présent document et les documents contractuels officiels, ces derniers ont préséance.". – DillPickleSwimmer Jul 27 '16 at 14:42

1 Answers1

0

I ended up finding the solution to my problem:

I could not set and stuff in the at gets overwritten by another program that modifies the file that I do not have any control over so French characters have to be displayed in the text as is.

To display properly had to do

fwrite($file, utf8_decode($a)."\r\n");

Thanks everyone :)