在PHP中解析字符串

时间:2015-11-23 11:40:52

标签: php

exports.getOrders = function(req, res) {
    Order.find({})
        .populate('customer')
        .populate({path: 'referencePeople', model: 'customer'})
        .exec(function(error, orders) {
            if(error) {
                return res.status(400).send({msg: 'Error occurred while getting orders.', error: error});
            }
            return res.status(200).send(orders);
        });
};

我想解析这个字符串并需要

  

标记= lTQ4sJx9vK7pR7kgeYVDgQ急症= 1448284525&安培; U = 37997

在PHP中有可能这样做吗?

{"status":"ok","params":{"stream_token":"token=lTQ4sJx9vK7pR7kgeYVDgQ&e=1448284525&u=37997"}}

引发以下错误:

  

捕获致命错误:无法转换类stdClass的对象   字符串

1 个答案:

答案 0 :(得分:3)

将额外参数添加到json_decode以将结果检索为关联数组:

$data = json_decode('{"status":"ok","params":{"stream_token":"token=U8h5Ma12SrlizPoFm-Nc5w&e=1448285819&u=37997"}}', TRUE);
$url= $data['params']['stream_token'];    
var_dump($url); // token=U8h5Ma12SrlizPoFm-Nc5w&e=1448285819&u=37997"

要解析网址变量,请使用parse_str

parse_str($url, $fragments);
var_dump($fragments);//
/*
array(3) {
  ["token"]=>
  string(22) "U8h5Ma12SrlizPoFm-Nc5w"
  ["e"]=>
  string(10) "1448285819"
  ["u"]=>
  string(5) "37997"
}
*/