2016-04-30 1 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_split и array_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