php preg match仅获取最后匹配的字符串

php preg match仅获取最后匹配的字符串,php,curl,preg-match,explode,Php,Curl,Preg Match,Explode,我需要获取最后一个HTTP头。我的字符串是: HTTP/1.1 302 Moved Temporarily Date: Sat, 30 Apr 2016 09:48:56 GMT Server: Apache X-Powered-By: PHP/5.5.34 Location: 2.php Content-Length: 0 Content-Type: text/html HTTP/1.1 302 Moved Temporarily Date: Sat, 30 Apr 2016 09:48:5

我需要获取最后一个HTTP头。我的字符串是:

HTTP/1.1 302 Moved Temporarily
Date: Sat, 30 Apr 2016 09:48:56 GMT
Server: Apache
X-Powered-By: PHP/5.5.34
Location: 2.php
Content-Length: 0
Content-Type: text/html

HTTP/1.1 302 Moved Temporarily
Date: Sat, 30 Apr 2016 09:48:57 GMT
Server: Apache
X-Powered-By: PHP/5.5.34
Location: 3.php
Content-Length: 0
Content-Type: text/html

HTTP/1.1 200 OK
Date: Sat, 30 Apr 2016 09:48:57 GMT
Server: Apache
X-Powered-By: PHP/5.5.34
Transfer-Encoding: chunked
Content-Type: text/html
但我需要得到最后的标题。我试图用\n\n分解此字符串,但无法得到结果。可以用preg_match做吗?

明白了! 我需要用以下代码将其分解:

explode("\r\n\r\n", $header);

使用
preg\u split
array\u pop
功能的解决方案:

// $headers is your initial string
$headers_splitted = preg_split("/\R{2}/", $headers);

print_r(array_pop($headers_splitted));
输出:

HTTP/1.1 200 OK
Date: Sat, 30 Apr 2016 09:48:57 GMT
Server: Apache
X-Powered-By: PHP/5.5.34
Transfer-Encoding: chunked
Content-Type: text/html