Nirali Joshi Nirali Joshi - 7 months ago 27
PHP Question

extract parameter from response

I am getting this string from CURL response

{status: 'false',description:'5400'}


I want to extract description parameter only.
I want $description = 5400 ;

How to do this ?

Answer

You have a bad json response, you have to change your json code and than using the following code you can access the variable.

<?php
$response = '{"status":false,"description":5400}';
$response_array = json_decode($response);
echo $response_array->description;
?>

And if you want to get the value of description with the existing bad json, you can do following thing.

<?php
$response = "{status:'false',description:'5400'}";
$r = explode("description:'", $response);
$description = rtrim($r[1],"'}");
echo $description;
?>