2016-04-30 54 views
-1

我需要獲取最後一個HTTP標頭。我的字符串是:php preg match只獲取最後匹配的字符串

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可以做到嗎?

回答

0

嘿! 我需要與此代碼爆炸它:

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

使用preg_splitarray_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