tags:

views:

56

answers:

2

Hello,

I am parsing a JSON response (twitter api - cursor value) and what should be a string value seems to be a double value when I output it with PHP.

Any idea how I get the real string value?

+1  A: 

To convert a float (or any type of variable) to a string, you can use one of these:

$value = 5.234;

// Using type casting
$str_value = (string)$value;

// Using strval()
$str_value = strval($value);

// Using concatenation to a string
$str_value = $value . '';

// Using sprintf
$str_value = sprintf("%s", $value);

// Using setType
setType($value, 'string');
$str_value = $value;
Tatu Ulmanen
A: 

The curser value is too large for 32bit PHP installs to handle with json_decode. Someone sent me preg_replace( '/next_cursor":(\d+)/', 'next_cursor":"\1"', $json );. Running that before json_decode will convert the json int to a string before conversion.

abraham
It's not too big - it's just being displayed in scientific notation...
gnud
"Scientific notation is a way of writing numbers that accommodates values too large or small to be conveniently written in standard decimal notation." - http://en.wikipedia.org/wiki/Scientific_notation
abraham