0

我遇到了一个问题,我有一个包含完整描述的字符串以及一个包含一些 Id 的 json。如何将 Json 从字符串中取出并对其执行任何事件..

我的数据如下所示

The description of xxxxxxxxxxxxxxxxxxxxxxxxxx
[{"id":"613"},{"id":"614"},{"id":"615"}]

有什么方法可以让我拥有完整的描述并拥有 ID,以便我可以解码它们并在我想要的地方使用?

预先感谢您的支持

4

2 回答 2

1

尝试这个

$string = 'The description of xxxxxxxxxxxxxxxxxxxxxxxxxx    [{"id":"613"},{"id":"614"},{"id":"615"}] asdasd';

if(false !== preg_match('/\[(.*)\]/', $string, $matches)) {
    for($n = 1; $n < count($matches); $n++) {
        $json_result = json_decode('['.$matches[$n].']', true);
        if(null === $json_result) {
            //cannot parse json
        }
        print_r($json_result);
    }
}
于 2012-11-13T08:26:03.203 回答
0

如果您的 json 在新行中,这将找到具有 JSON 代码的第一行!!!

$string = 'The description of xxxxxxxxxxxxxxxxxxxxxxxxxx
[{"id":"613"},{"id":"614"},{"id":"615"}]';
$strings = explode("\n",$string);

foreach($strings as $str){
    $json = json_decode($str,TRUE); //TRUE for array responde
    if(!empty($json)){
       break;
    }
}

var_dump($json);
于 2012-11-13T08:28:18.823 回答