Skip to content

Instantly share code, notes, and snippets.

@iprodev
Last active December 29, 2018 11:12
Show Gist options
  • Save iprodev/472179039c856b370106eb4e6470b174 to your computer and use it in GitHub Desktop.
Save iprodev/472179039c856b370106eb4e6470b174 to your computer and use it in GitHub Desktop.
PHP : Reads entire file into a string without UTF8/UTF16 bom
<?php
/**
* Reads entire file into a string without UTF8/UTF16 bom
*
* @param string $file Path of the file to read.
* @return string The read data or FALSE on failure.
*/
function file_get_contents_utf( $file = null ) {
if ( is_readable( $file ) ) {
if ( !( $fh = fopen( $file, 'r' ) ) ) return false;
$data = fread( $fh, filesize( $file ) );
// remove bom
$bom = pack( 'H*','EFBBBF' );
$data = preg_replace( "/^$bom/", '', $data );
fclose( $fh );
return $data;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment