RE: [PHP-INSTALL] Readfile

From: Date: Mon, 27 Sep 2004 11:32:55 +0000
Subject: RE: [PHP-INSTALL] Readfile
Groups: php.install 
Request: Send a blank email to php-install+get-14432@lists.php.net to get a copy of this message
Heh, I was thinking of something like your first Solution during the weekend. But yours was a lot better. Scott -----Original Message----- From: Brad Kowalczyk [mailto:brad@ibiscode.com] Sent: Saturday, September 25, 2004 7:40 PM To: Scott Hamm Cc: Php-install Subject: Re: [PHP-INSTALL] Readfile 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 |> brad@ibiscode.com |> www.ibiscode.com

« previous php.install (#14432) next »