2016-04-30 64 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

Gotcha! 私はこのコードでそれを爆発する必要があります。

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 
関連する問題