检查PHP中较大字符串内的JSON字符串

时间:2018-08-01 18:04:21

标签: php json regex

以下是日志文件条目的示例:

[22-Aug-2017 16:19:58 America/New_York] WP_Community_Events::maybe_log_events_response: Valid response received. Details: {"api_url":"https:\/\/api.wordpress.org\/events\/1.0\/","request_args":{"body":{"number":5,"ip":"192.168.99.0","locale":"en_GB","timezone":"America\/New_York"}},"response_code":200,"response_body":{"location":{"ip":"47.197.97.47"},"events":"5 events trimmed."}}

{"api_url":"https:\/\/api.wordpress....部分是有效的JSON,但显然整个字符串本身(日志输入行)不是。我在想办法从本质上拉出JSON而又不会弄乱字符串的任何其他部分。

3 个答案:

答案 0 :(得分:1)

您可以尝试获取json对象的开始和结束并将其解码。如果没有json解码错误,那就很好。假设字符{}仅在json正文中使用。

function checkIfStringHasValidJson($string)
{
    $start = strpos($string, '{');
    $end = strrpos($string, '}');
    $json = substr($string, $start, $end);
    json_decode($json);
    return json_last_error() === JSON_ERROR_NONE;
}

答案 1 :(得分:1)

使用preg_match_all查找所有JSON并将其存储到数组中,如下所示:

$text = '[22-Aug-2017 16:19:58 America/New_York] WP_Community_Events::maybe_log_events_response: Valid response received. Details: {"api_url":"https:\/\/api.wordpress.org\/events\/1.0\/","request_args":{"body":{"number":5,"ip":"192.168.99.0","locale":"en_GB","timezone":"America\/New_York"}},"response_code":200,"response_body":{"location":{"ip":"47.197.97.47"},"events":"5 events trimmed."}}';

preg_match_all('/\{(?:[^{}]|(?R))*\}/x', $text, $matches);

echo '<pre>';
print_r($matches[0]);

这将产生:

Array
(
    [0] => {"api_url":"https:\/\/api.wordpress.org\/events\/1.0\/","request_args":{"body":{"number":5,"ip":"192.168.99.0","locale":"en_GB","timezone":"America\/New_York"}},"response_code":200,"response_body":{"location":{"ip":"47.197.97.47"},"events":"5 events trimmed."}}
)

您可以从以下网站阅读更多内容: Extracting the JSON string from given text

或者,如果您想要相反的操作并删除JSON并保留字符串,则可以使用preg_replace来做到这一点:

$text = '[22-Aug-2017 16:19:58 America/New_York] WP_Community_Events::maybe_log_events_response: Valid response received. Details: {"api_url":"https:\/\/api.wordpress.org\/events\/1.0\/","request_args":{"body":{"number":5,"ip":"192.168.99.0","locale":"en_GB","timezone":"America\/New_York"}},"response_code":200,"response_body":{"location":{"ip":"47.197.97.47"},"events":"5 events trimmed."}}';

$cleantext = preg_replace('~\{(?:[^{}]|(?R))*\}~', '', $text);

echo $cleantext;

来自PHP: How to extract JSON strings out of a string dump的信用

这将产生:

[22-Aug-2017 16:19:58 America/New_York] WP_Community_Events::maybe_log_events_response: Valid response received. Details:

答案 2 :(得分:0)

只需从字符串中删除“ Details:”的给定子字符串(包括该子字符串)之前的所有内容:

$s = '[22-Aug-2017 16:19:58 America/New_York] WP_Community_Events::maybe_log_events_response: Valid response received. Details: {"api_url":"https:\/\/api.wordpress.org\/events\/1.0\/","request_args":{"body":{"number":5,"ip":"192.168.99.0","locale":"en_GB","timezone":"America\/New_York"}},"response_code":200,"response_body":{"location":{"ip":"47.197.97.47"},"events":"5 events trimmed."}}';
$out = str_replace('Details: ', '', strstr($s, 'Details:'));
echo var_export(json_decode($out));