utf 8 - Parsing data from file with PHP -


i've strange issue parsing data file file_get_contents.

file contents: 0,0,0 (there 3 random numbers in range 0 255 separated comma)

$s = file_get_contents($this->filepath); var_dump(trim($s)); $rgb = explode(',', trim($s)); var_dump($rgb); 

output:

string(8) "0,0,0" array(3) {   [0]=>   string(4) "0"   [1]=>   string(1) "0"   [2]=>   string(1) "0" } 

and if try converting integer:

string(14) "255,128,255" array(3) {   [0]=>   string(6) "255"   [1]=>   string(3) "128"   [2]=>   string(3) "255" }  echo((int)$rgb[0]); // -> 0 echo((int)$rgb[1]); // -> 128 echo((int)$rgb[2]); // -> 255 

the file in encoding utf-8 , think there whitespace char in parsed string, don't know should it, cause simple trim not work.

thanks ideas!

your file begins byte order mark (bom). length of both strings giveaway, both 3 bytes long.

ideally, should attempt remove problem @ source, saving file without bom @ start. however, if not option, can strip manually string:

$str = file_get_contents($this->filepath); $bom = pack("ccc", 0xef, 0xbb, 0xbf); if (strncmp($str, $bom, 3) === 0) {     $str = substr($str, 3); } 

i've used modified version of code taken this blog post. basically, compares first 3 bytes of string bom , removes them string if necessary.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -