Skip to content

Instantly share code, notes, and snippets.

@xeiter
Created August 13, 2016 03:39
Show Gist options
  • Save xeiter/d2fe5fb4b83cd9a3da5f9397fac9573c to your computer and use it in GitHub Desktop.
Save xeiter/d2fe5fb4b83cd9a3da5f9397fac9573c to your computer and use it in GitHub Desktop.
Fix UTF-8 in CSV output
<?php
// Open file pointer to standard output
$fp = fopen( 'php://output', 'w' );
// Write BOM character sequence to fix UTF-8 in Excel
fputs( $fp, $bom = chr(0xEF) . chr(0xBB) . chr(0xBF) );
// Write the rest of CSV to the file
if ( $fp ) {
fputcsv( $fp, array( "Cars", "Planes", "Ships" ), ";" );
fputcsv( $fp, array( "12", "2", "6" ), ";" );
fputcsv( $fp, array( "23", "3", "5" ), ";" );
fputcsv( $fp, array( "31", "5", "8" ), ";" );
}
fclose( $fp );
@chrismok
Copy link

Thank you

@symplTech
Copy link

Worked like a charm. Thanks!

@dtrifonoff
Copy link

Still working well in 2024 ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment