如何在php中检测字符串中的Json

如何在php中检测字符串中的Json,php,arrays,json,Php,Arrays,Json,我陷入了一个问题,我有一个完整描述的字符串和一个包含一些ID的json。如何将Json从字符串中取出并对其执行任何事件 我的数据如下 The description of xxxxxxxxxxxxxxxxxxxxxxxxxx [{"id":"613"},{"id":"614"},{"id":"615"}] 有没有什么方法可以让我有完整的描述和ID,这样我就可以解码它们并在我想要的地方使用 提前感谢您的支持试试这个 $string = 'The description of xxxxxxxxxx

我陷入了一个问题,我有一个完整描述的字符串和一个包含一些ID的json。如何将Json从字符串中取出并对其执行任何事件

我的数据如下

The description of xxxxxxxxxxxxxxxxxxxxxxxxxx
[{"id":"613"},{"id":"614"},{"id":"615"}]
有没有什么方法可以让我有完整的描述和ID,这样我就可以解码它们并在我想要的地方使用

提前感谢您的支持

试试这个

$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);
    }
}
$string='xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx[{“id”:“613”},{“id”:“614”},{“id”:“615”}]asdasd'的描述;
if(false!==preg\u match('/\[(.*)\]/',$string,$matches)){
对于($n=1;$n
如果您的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);

如果描述不总是只有一行,或者存在任何其他适当的限制(例如不包含
{
使该字符始终成为第一个JSON字符),您可能必须使用暴力解决方案。请参阅示例(用JavaScript编写,但可以将其移植到PHP),它们是否总是用新行分隔?