Re: Readfile
| From: | Brad Kowalczyk | Date: | Sat, 25 Sep 2004 23:39:35 +0000 |
| Subject: | Re: Readfile | ||
| References: | 1 | Groups: | php.install |
| Request: | Send a blank email to php-install+get-14424@lists.php.net to get a copy of this message | ||
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