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.
开发者_StackOverflow中文版Any idea how I get the real string 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.
Update: Twitter now provides next_cursor_str values that are strings instead of integers so using preg_replace
is no longer needed.
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;
精彩评论