0

完全な説明を含む文字列と、いくつかの ID を含む json があるという問題で立ち往生しています。Json を String から取り出して、その上でイベントを実行するにはどうすればよいですか?

私のデータは次のようになります

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コードを持つ最初のLINEを見つけます!!!

$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 に答える