在PHP中检查较大字符串中的JSON字符串
以下是日志文件条目的示例:在PHP中检查较大字符串中的JSON字符串,php,json,regex,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","lo
[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\u url”:“https:\/\/api.wordpress….
部分是有效的JSON,但显然是整个字符串本身(日志条目行)不是。我遇到了一个难题,可以在不弄乱字符串任何其他部分的情况下基本上拉出JSON。只需删除前面字符串中的所有内容,并包括给定的子字符串“Details:”:
您可以尝试获取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;
}
用于查找所有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]);
您可以从以下网站了解更多信息:
或者,如果您想要相反的结果并删除JSON并保留字符串,则可以使用:
[22-Aug-2017 16:19:58 America/New_York] WP_Community_Events::maybe_log_events_response: Valid response received. Details:
归功于
这将产生:
$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;
看起来你可以在第一个大括号之前剪切所有内容,在最后一个大括号之后剪切所有内容。我想JSON是在
细节之后:
我想。这是一个提示。这是一个例子。它并不总是在前面有“细节”(或其他相关内容),它想说这也行得通,但preg_match_all会得到所有细节(如果不止一个存在)@Norcross是有道理的,我假设只有一个物体,并且故意让它保持光亮。很高兴你有了一个解决方案!
$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;
[22-Aug-2017 16:19:58 America/New_York] WP_Community_Events::maybe_log_events_response: Valid response received. Details: