Question

I am reading a UTF8 file and storing data read from file in an array. However, when using that data in mysql queries, I am getting problems. I thought that I will convert all int values explicitly using intval(..) before using them. But intval(utf8string) is giving me zero.

Following code gives unexpected output:

 //.....
 var_dump($data[10]);
 echo '|' . $data[10] . "|\n";  
 echo '|' . bin2hex($data[10]) . "|\n";
 echo '|' . utf8_decode($data[10]) . "|\n";
 echo '|' . intval(utf8_decode($data[10])) . "|\n";
 echo '|' . intval($data[10]). "|\n"; exit
 //.....

Output:

string(15) "2240000"
|2240000|
|003200320034003000300030003000|
|2240000|
|0|
|0|

Any idea how to get correct intval from such data?

Was it helpful?

Solution

Looks like your string is encoded in UTF-16, not UTF-8.

Either change something about where you get the data from, or convert the encoding:

$data[10] = iconv('UTF-16', 'UTF-8', $data[10])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top