Help - Search - Members - Calendar
Full Version: Little regex help needed...
Invision Power Services > Community Forums > Community Web Design and Coding
ellawella
For a project I'm working on, I need a function which grabs a page off an external web server and shows the result on screen. I achieved this initially with fsockopen(), fgets() etc. but ran into the obstacle of having my function output the response headers from the web server the request was made to as well as the actual content of the page. Annoying.

Then I stumbled across this function (from php.net) which somehow filters out the headers:

CODE
function sock_get_contents($ip, $port, $file, $timeout, $header = false)
{
   $request = "GET ".$file." HTTP\1.0\r\n\r\n";

   $return = '';

   $sock = fsockopen($ip, $port, $err_no, $err_str, $timeout);

   if(is_resource($sock))
   {
       fputs($sock, $request);
       if($header == false)
       {
           $line = "";
           while(!($line == "\r\n"))
           {
               $line = fgets($sock, 128);

               if(preg_match("#Location\: (.*?)\\r\\n#si", $line, $matches))
               {
                   return sock_get_contents($ip, $port, "/".$matches[1], $timeout, $header);
               }
           }    // strip out the header
       }
       while ($buffer = fgets($sock, 4096))
       {
           $return .= $buffer;
       }

       fclose($sock);

       return $return;
   }
   else
   {
       return false;
   }
}

The line with preg_match holds a regex which magically filters out all headers. My question is: how does it do it? It looks fiendishly complex and for the purpose of expanding my knowledge I wondered whether anyone could provide a short explanation. original.gif
Rikki
You can be sure that headers on a webpage are ended by a double-linebreak, so it's easy to get everything before the first double-linebreak, and that would be the headers. So the regex is getting everything from Location: (the first header, I assume), to the last single-linebreak. From then on it has the content only.
mandos
nvm, missunderstood the question
ellawella
Many thanks! thumbsup.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.