Created
August 13, 2016 03:39
-
-
Save xeiter/d2fe5fb4b83cd9a3da5f9397fac9573c to your computer and use it in GitHub Desktop.
Fix UTF-8 in CSV output
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
Worked like a charm. Thanks!
Still working well in 2024 ;)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you