Save txt file with windows encoding

Asked

Viewed 485 times

0

My application runs on a Unix OS, so my files .TXT are created in UNIX format, I need these files to be created in WINDOWS format.

I need the line breaks to be in WINDOWS format r n. I’m having trouble with UNIX format because it only inserts n.

Today, I create the files using file_put_contents.

  • 4

    Can you describe it better? What exactly do you want to change in shape? Line breaks? Charset (accent)? Please be more specific.

  • I think he refers to line breaks

  • That, I mean line breaking. I need line breaks to be in WINDOWS r n format. I’m having trouble with UNIX format because it only inserts n.

  • 1

    @Danilooliveira are you writing in the files? If yes just write r n.

  • @Jorgeb, yes, I’m the one who’s writing. Writing n doesn’t work, I’ve taken this test.

2 answers

1

For this it is enough that whenever you save something in the file you already go with the correct formatting, using the function iconv:

<?php

$dados = "Eu não conseguia guardar texto em formato windows e agora já consigo.";

// Alterar o enconding do texto a gravar no ficheiro
$string_encoded = iconv( mb_detect_encoding( $dados ), 'Windows-1252', $dados );

// Escrever no ficheiro
$file = fopen( "meu_texto.txt", "w+" );
fwrite( $file, $string_encoded );
fclose( $file );

0

You can write the file by adding the line breaks manually with the characters #13 + #10 using the function chr().

  • Thanks for the answer, Wellington. I tried the way you suggested and still did not get the result I expected. Even passing the ascii characters, my file is in UNIX format, so it only writes the LF in line break, if I change the file format by Notepad++ to DOS/Windows it writes CRLF, this is how I expect the file to be generated.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.