Scott Hamm wrote:

How can I set a limit on readfile() function to only read contents between
<body> and </body> tags rather than entire file?


I dont think you can but here's two solutions (not using readfile()):

Solution 1.

//beware that any other text on same line as body tags will be lost

$fh = fopen($filename, 'r');
$body_contents = '';
$read = false;

while(!feof($fh))
{
   $line = fgets($fh);
   if (preg_match('/<\/body>/', $line))
        $read = false;
   if ($read)
      $body_contents .= $line;
   if (preg_match('/<body(\s.*?)?>/', $line))
        $read = true;
}

echo $body_contents;


Solution 2
// should extract all text between body tags even if on same line
// I'm far from a regex guru so there may well be a better pattern than this :)


$file_string = file_get_contents($filename);
$file_string = preg_replace("/.+?<body(\s.*?)?>(.+?)<\/body>.+/is", '$2', $file_string);
echo $file_string;


--

|> Brad Kowalczyk
|> Web Developer
|> [EMAIL PROTECTED]
|> www.ibiscode.com

Reply via email to