Hi all,

I know it sounds counter-productive, but I'm actually trying to convert text from UTF-8 to "MS-ANSI" or "Windows-1252". I'm parsing an RSS feed and spitting out an Excel compatible CSV file (long story). Anyway, I have the following PHP function:

function formatForExcel($fp) {

  $str = readFile($fp);  # Custom Function; Read entire file from file pointer and save in $str
  $str = iconv("UTF-8", "WINDOWS-1252//TRANSLIT", $str);  # Do the conversion

  # Convert LF (Unix) to CRLF (Windows)
  $str = preg_replace("/\n/", "\r\n", $str);

  rewind($fp);
  saveFile($fp, $str);  # Custom Function; Save the file 
  return 1;  
}

My code works fine except the encoding doesn't appear to be changing on the output file. When I open it in "KATE" (KDE) or Notepad++ (Windows), it still shows as UTF-8.

Instead of iconv, I have also tried:

$str = mb_convert_encoding($str, 'windows-1252', 'utf-8');

Thoughts? Grateful for the help, as this is driving me nuts!

    So, you're downloading the file from a web server?

      It pulls an XML file (an RSS feed) from one of our servers, parses the XML and then converts it to a CSV. The new CSV file is then uploaded to a remote 3rd party FTP server. The software on the remote FTP server is essentially a hot folder (an input) that expects an Excel compatible "windows-1252" encoded CSV file. The file that ends up on the remote FTP server can be opened in LibreOffice, which reports it as "windows-1252", but Notepad++ on Windows and KATE on KDE (Linux) both report the file is UTF-8.

        I should note that I'm only using LibreOffice, Notepad++ and KATE to validate the encoding, as well as the Windows compatible line returns (CRLF).

          I've got it figured out. In reality, I have nothing wrong with my code. Because "windows-1252" is a subset of UTF-8, Notepad++ has no idea it isn't already UTF-8, and thus has no reason not to be in the mode of using UTF-8 for further typing. Basically I have a non-issue.

            Well, ya gotta love resolutions like that ...

            ... as long as they don't take years off your life and hair off your scalp. 😉

            Welcome to PHPBuilder, BTW. 🙂

              Write a Reply...