HTTP/1.1 200 OK<br>Server: NWSs<br>Date: Sat, 02 Sep 2017 05:31:58 GMT<br>Content-Type: text/html; charset=utf-8<br>Content-Length: 4955<br>Connection: keep-alive<br>Cache-Control: max-age=86400<br>Expires: Sun, 03 Sep 2017 05:31:58 GMT<br>Last-Modified: Thu, 31 Aug 2017 08:29:39 GMT<br>Content-Encoding: gzip<br>X-NWS-LOG-UUID: b5d7cf9d-5d88-44ba-a6d0-411024e5ffc1<br>X-Cache-Lookup: Hit From MemCache Gz<br>Access-Control-Allow-Origin: *<br><br>
我需要分解成array("Server" => "NWSs", "Date" => "Sat, 02 Sep 2017 05:31:58 GMT"); 这样的!
3 回答
aluckdog
TA贡献1847条经验 获得超7个赞
$header = 'HTTP/1.1 200 OK<br>Server: NWSs<br>Date: Sat, 02 Sep 2017 05:31:58 GMT<br>Content-Type: text/html; charset=utf-8<br>Content-Length: 4955<br>Connection: keep-alive<br>Cache-Control: max-age=86400<br>Expires: Sun, 03 Sep 2017 05:31:58 GMT<br>Last-Modified: Thu, 31 Aug 2017 08:29:39 GMT<br>Content-Encoding: gzip<br>X-NWS-LOG-UUID: b5d7cf9d-5d88-44ba-a6d0-411024e5ffc1<br>X-Cache-Lookup: Hit From MemCache Gz<br>Access-Control-Allow-Origin: *<br><br>';
$header = explode('<br>', $header);
$method = array_shift($header);
$headers = [];
foreach ($header as $item) {
preg_match_all("/(.*?): (.*)/", $item, $arr);
if (!$arr[1][0] || !$arr[2][0]) {
continue;
}
$headers[$arr[1][0]] = $arr[2][0];
}
纯手打,未验证。要是有错可以稍微改一下。大致上是这样。
小唯快跑啊
TA贡献1863条经验 获得超2个赞
别忘了 PHP 是一个合格的脚本语言,这些基本的功能实现起来是非常简单。今日得闲,帮你尝试一番,采用最基本的数组和字符串之间的转换和拼接,过程如下:
<?php
$raw = 'HTTP/1.1 200 OK<br>Server: NWSs<br>Date: Sat, 02 Sep 2017 05:31:58 GMT<br>Content-Type: text/html; charset=utf-8<br>Content-Length: 4955<br>Connection: keep-alive<br>Cache-Control: max-age=86400<br>Expires: Sun, 03 Sep 2017 05:31:58 GMT<br>Last-Modified: Thu, 31 Aug 2017 08:29:39 GMT<br>Content-Encoding: gzip<br>X-NWS-LOG-UUID: b5d7cf9d-5d88-44ba-a6d0-411024e5ffc1<br>X-Cache-Lookup: Hit From MemCache Gz<br>Access-Control-Allow-Origin: *<br><br>';
// 以 <br> 为界初步分割为数组
$str = explode('<br>', $raw);
// 去除第一个无用元素 和 最后两个空元素
array_shift($str);
array_pop($str);
array_pop($str);
// 创建新数组用于储存拼接内容
$result = [];
foreach ($str as $key => $value) {
$single = explode(':', $value);
// 以下始拼接,防止被时间那组数据坑了
// 获取新数组的关联
$result_key = $single[0];
// 去除关联,将剩余元素使用:接成为字符串
array_shift($single);
$result_value = implode(':', $single);
// 储存处理好的内容
$result = array_merge($result, ["$result_key" => "$result_value"]);
}
//输出结果
print_r($result);
?>
在 PHP 7.1.4 中,输出结果如下,基本满足题主的需求,并且避开了时间的坑:
Array
(
[Server] => NWSs
[Date] => Sat, 02 Sep 2017 05:31:58 GMT
[Content-Type] => text/html; charset=utf-8
[Content-Length] => 4955
[Connection] => keep-alive
[Cache-Control] => max-age=86400
[Expires] => Sun, 03 Sep 2017 05:31:58 GMT
[Last-Modified] => Thu, 31 Aug 2017 08:29:39 GMT
[Content-Encoding] => gzip
[X-NWS-LOG-UUID] => b5d7cf9d-5d88-44ba-a6d0-411024e5ffc1
[X-Cache-Lookup] => Hit From MemCache Gz
[Access-Control-Allow-Origin] => *
)
综上所述,题目并不困难,只要肯思考肯定可以解决,题主可以封装为一个函数直接使用即可。唯一的小小小坑出现在时间分割处,注意克服即可。
牧羊人nacy
TA贡献1862条经验 获得超7个赞
先根据<'br'>分割出数组,array('HTTP/1.1 200 OK','Server: NWSs','Date: Sat, 02 Sep 2017 05:31:58 GMT','Content-Type: text/html; charset=utf-8',......),然后再做处理,拿出你想要的内容
- 3 回答
- 0 关注
- 417 浏览
添加回答
举报
0/150
提交
取消