sczdavos
sczdavos

Reputation: 2063

Parsing data from file with PHP

I've a strange issue with parsing data from file with file_get_contents.

File contents: 0,0,0 (there are 3 random numbers in range from 0 to 255 separated by 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 I try converting to 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 is in encoding UTF-8 and I think there is some whitespace char in the parsed string, but I don't know what should I do with it, cause simple trim does not work.

Thanks for ideas!

Upvotes: 0

Views: 46

Answers (1)

Tom Fenech
Tom Fenech

Reputation: 74675

Your file begins with a Byte Order Mark (BOM). The length of both your strings is a giveaway, as they are both 3 bytes too long.

Ideally, you should attempt to remove this problem at the source, by saving your file without the BOM at the start. However, if this is not an option, you can strip it manually from your 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 a very slightly modified version of some code taken from this blog post. Basically, it compares the first 3 bytes of the string with the BOM and removes them from the string if necessary.

Upvotes: 1

Related Questions