Last active
December 29, 2018 11:12
-
-
Save iprodev/472179039c856b370106eb4e6470b174 to your computer and use it in GitHub Desktop.
PHP : Reads entire file into a string without UTF8/UTF16 bom
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 | |
/** | |
* 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