从响应中提取参数

时间:2016-04-28 05:45:16

标签: php curl

我从CURL响应中获取此字符串

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

我只想提取描述参数。 我想$ description = 5400;

怎么做?

3 个答案:

答案 0 :(得分:0)

那回应是json。把它变成一个PHP数组:

$response = "{status: 'false',description:'5400'}";
$response_array = json_decode($response, true);
$description = $response_array['description'];
echo $description;

答案 1 :(得分:0)

你有一个糟糕的json响应,你必须改变你的json代码,而不是使用下面的代码你可以访问变量。

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

如果你想用现有的坏json获取描述的值,你可以做以下事情。

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

答案 2 :(得分:0)

@ Nirali Joshi

如果您的JSON带有空格,那么它将是写解决方案

$ response =“{status:'false',说明:'5400'}”;

$ response = str_replace(“”,“”,$ response);

OR

$ response = preg_replace('/ \ s + /','',$ response);

$ t_repsonse = explode(“description:”,$ response);

$ i_description = str_replace(“}”,“”,str_replace(“'”,“”,$ t_repsonse [1]));

OR

$ i_description = str_replace(“}”,“”,preg_replace('([“\']')','',$ t_repsonse [1]));

$ description = $ i_description;

对不起,如果发现任何错误,请改进并感谢

相关问题