Hello everyone!

We have been investing plenty of personal time and energy for many years to share our knowledge with you all. However, we now need your help to keep this blog running. All you have to do is just click one of the adverts on the site, otherwise it will sadly be taken down due to hosting etc. costs. Thank you.

You can use example below to split values into named array keys by using preg_match function.


Input


You need to flatten this.


{
"time_local": "2019-06-21T23:30:14+0000",
"client_ip": "-",
"remote_addr": "192.168.240.3",
"remote_user": "",
"request": "GET / HTTP/1.1",
"status": "200",
"body_bytes_sent": "0",
"request_time": "0.001",
"http_referrer": "",
"http_user_agent": "curl/7.38.0",
"request_id": "8bf75860a56458f1e37a13d1178d8938"
}

Pattern


/^{"time_local":"(?<time_local>.*?)","client_ip":"(?<client_ip>.*?)","remote_addr":"(?<remote_addr>.*?)","remote_user":"(?<remote_user>.*?)","request":"(?<request>.*?)","status":"(?<status>.*?)","body_bytes_sent":"(?<body_bytes_sent>.*?)","request_time":"(?<request_time>.*?)","http_referrer":"(?<http_referrer>.*?)","http_user_agent":"(?<http_user_agent>.*?)","request_id":"(?<request_id>.*?)"}$/

Test


preg_match($pattern, $input, $matches);

Content of $matches variable. Full example is here.


Array
(
[0] => {"time_local":"2019-06-21T23:30:14+0000","client_ip":"-","remote_addr":"192.168.240.3","remote_user":"","request":"GET / HTTP/1.1","status":"200","body_bytes_sent":"0","request_time":"0.001","http_referrer":"","http_user_agent":"curl/7.38.0","request_id":"8bf75860a56458f1e37a13d1178d8938"}
[time_local] => 2019-06-21T23:30:14+0000
[1] => 2019-06-21T23:30:14+0000
[client_ip] => -
[2] => -
[remote_addr] => 192.168.240.3
[3] => 192.168.240.3
[remote_user] =>
[4] =>
[request] => GET / HTTP/1.1
[5] => GET / HTTP/1.1
[status] => 200
[6] => 200
[body_bytes_sent] => 0
[7] => 0
[request_time] => 0.001
[8] => 0.001
[http_referrer] =>
[9] =>
[http_user_agent] => curl/7.38.0
[10] => curl/7.38.0
[request_id] => 8bf75860a56458f1e37a13d1178d8938
[11] => 8bf75860a56458f1e37a13d1178d8938
)