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?
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?
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;
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.