Re: HTTP Header Viewer... Do I need regexps?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Leif Gregory wrote:

Hello everyone,

I've been putzing with this for about a week on and off now.

  I've googled like crazy and tried to figure out the regexps, but I
  can't just get it.

  What I'm doing is taking some code I found to show me the HTTP
  headers from a remote server and making it a bit more dynamic with a
  runtime input, and to format the output nicely. I can retrieve the
  headers just fine, but I can't get the output broken out into pieces
  from the returned single line.

i.e.
HTTP/1.1 200 OK Date: Tue, 20 Apr 2004 17:28:23 GMT Server: Microsoft-IIS/5.0 Last-modified: Thu, 01 Jan 2004 19:56:39 GMT Connection: close Content-type: text/html


  I want to format it as such by breaking each line into its own
  array element:

To split a string into an array you can use preg_split() or split(). http://www.php.net/manual/en/function.preg-split.php http://www.php.net/split



HTTP/1.1 200 OK Date: Tue, 20 Apr 2004 17:28:23 GMT Server: Microsoft-IIS/5.0 Last-modified: Thu, 01 Jan 2004 19:56:39 GMT Connection: close Content-type: text/html

Any help would really be appreciated.


This will split up your request string. The key here is that you use parentheses in order to search for any item in the group followed by a colon and a space.


<?php

$header = 'HTTP/1.1 200 OK Date: Tue, 20 Apr 2004 17:28:23 GMT Server: Microsoft-IIS/5.0 Last-modified: Thu, 01 Jan 2004 19:56:39 GMT Connection: close Content-type: text/html';

$splitHTTP = array(
  'Date',
  'Server',
  'Last-Modified',
  'Connection',
  'Content-type',
);

$values = preg_split('/('.implode('|', $splitHTTP).'):\s/', $header);

echo '<pre>';
print_r($values);
echo '</pre>';

?>

--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [PHP Database Programming]     [PHP Install]     [Kernel Newbies]     [Yosemite Forum]     [PHP Books]

  Powered by Linux