2

I am reading in a CSV file (in UTF-8 encoding). One of the values is a number and when I try to cast it as an integer so I can do some calculations it is just being set to zero. What am I doing wrong?

var_dump ( $row3[_MM_Impressions] );
//writes: string(5) "59"

$imps = (int)$row3[_MM_Impressions];
var_dump($imps);
//writes: int(0) 

$imps = $row3[_MM_Impressions]*1;
var_dump($imps);
//writes: int(0) 

$imps = intval($row3[_MM_Impressions]);
var_dump($imps);
//writes: int(0) 
6
  • 1
    Do you know what the other 3 characters in the string are? Commented Dec 7, 2012 at 6:05
  • var_dump : string(5) : '59' doesn't look right to me either Commented Dec 7, 2012 at 6:06
  • %00%35%00%39%00 (this is the hex dump). Commented Dec 7, 2012 at 6:08
  • What is the true value $row3[__MM_Impressions] ? Commented Dec 7, 2012 at 6:18
  • Try removing the null bytes preg_replace("/\x00/", "" , $row3[_MM_Impressions]) Commented Dec 7, 2012 at 6:19

1 Answer 1

2

You have HEX string number in your vars... so why don't you use hexdec?

Returns the decimal equivalent of the hexadecimal number represented by the hex_string argument. hexdec() converts a hexadecimal string to a decimal number.

hexdec() will ignore any non-hexadecimal characters it encounters.

Sign up to request clarification or add additional context in comments.

1 Comment

Unfortunately it gets the wrong int number though. Vardump (original): string(5) "59" Vardump (after hexdec ($row3[_MM_Impressions])): int(89) int(89) here is another example that should be 49 as an int: string(5) "49" int(73) int(73)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.