如何用正则取出 http协议流 的头文件和文件体
2013-11-26 来自:鄭。囡 2 人回应

我有一个http协议流
like this:

如何用正则取出头文件部分和文件体?preg_match_all(.....)
跪谢

2013-11-27 来自:jordan2659

看标准:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html

每个http response header后跟CRLF, 也就是\r\n;
headers 和 body之间再有一个CRLF.

所以header和body之间会有两个CRLF, 以此判断就可以了. 示例如下:

<?php
$str= <<<EOF
HTTP/1.1 200 OK
Age: 547999
Date: Fri, 18 Oct 2013 01:29:42 GMT
Server: Golfe2
Expires: Wed, 19 Apr 2000 11:43:00 GMT
Alternate-Protocol: 80:quic
X-Content-Type-Options: nosniff

GIF89a
EOF;

echo "original string:\n";
var_dump($str);
preg_match_all('/(.*?)\r\n\r\n/s',$str,$matches);
echo "\nmatched string:\n";
var_dump($matches[1][0]);
?>

结果如下:

original string:
string(193) "HTTP/1.1 200 OK\r\nAge: 547999\r\nDate: Fri, 18 Oct 2013 01:29:42 GMT\r\nServer: Golfe2\r\nExpires: Wed, 19 Apr 2000 11:43:00 GMT\r\nAlternate-Protocol: 80:quic\r\nX-Content-Type-Options: nosniff\r\n\r\nGIF89a"

matched string:
string(183) "HTTP/1.1 200 OK\r\nAge: 547999\r\nDate: Fri, 18 Oct 2013 01:29:42 GMT\r\nServer: Golfe2\r\nExpires: Wed, 19 Apr 2000 11:43:00 GMT\r\nAlternate-Protocol: 80:quic\r\nX-Content-Type-Options: nosniff"
2013-11-27 来自:Stand by...

看标准:
http://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html

每个http response header后跟CRLF, 也就是\r\n;
headers 和 body之间再有一个CRLF.

所以header和body之间会有两个CRLF, 以此判断就可以了. 示例如下:

  
<?php
$str= <<<EOF
HTTP/1.1 200 OK
Age: 547999
Date: Fri, 18 Oct 2013 01:29:42 GMT
Server: Golfe2
Expires: Wed, 19 Apr 2000 11:43:00 GMT
Alternate-Protocol: 80:quic
X-Content-Type-Options: nosniff

GIF89a
EOF;

echo "original string:\n";
var_dump($str);
preg_match_all('/(.*?)\r\n\r\n/s',$str,$matches);
echo "\nmatched string:\n";
var_dump($matches[1][0]);
?>

您的回应

你还未登陆,不能回应!登陆