Re: including an HTML file
| From: | Randy Clamons | Date: | Fri, 14 Jan 2005 18:54:58 +0000 |
| Subject: | Re: including an HTML file | ||
| References: | 1 | Groups: | php.windows |
| Request: | Send a blank email to php-windows+get-25332@lists.php.net to get a copy of this message | ||
Gaetano,
Try using fread() instead of fgets(). You can read the entire file contents into a single variable.
You could then use a regular expression to extract the content. Something like this:
// get contents of a file into a string
$filename = "/usr/local/something.txt";
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
// pattern includes everything beginning with the opening body tag
// and ending with the closing body tag (tags included)
// s modifier allows a match across multiple lines
// i modifier to ignore case
$pattern ="/<body.*<\/body.*?>/si";
preg_match($pattern, $contents, $matches) ;
// $matches is an array with one element
echo $matches[0] ;
Randy Clamons
Systems Programming
Astro-auction.com
> ------------Original Message------------
> From: "Gaetano Savoca" <gaetanosavoca@inwind.it>
> To: php-windows@lists.php.net
> Date: Thu, Jan-13-2005 10:19 PM
> Subject: Re: [PHP-WIN] including an HTML file
>
> On Friday, January 14, 2005 1:06 AM [GMT+1=CET],
> Gaetano Savoca <gaetanosavoca@inwind.it> ha scritto:
>
> > So i must do all manually
> >
> > i write this
> >
> > <?php
> > if(file_exists($corpo)){// controll for the file
> > //Creating the filehandle for our input file
> > $fh=fopen("$corpo", "r");
> >
> > //Seeding the while loop below with the first line of the
> > input file
> > $line=fgets($fh);
> > while (!feof($fh)){
> > //Once we hit the <BODY> tag, start echoing out
> lines
> > until we hit </BODY>
> > if (preg_match("/<BODY/i", $line)){
> > while (!preg_match("/<\/BODY/i", $line =
> > fgets($fh))) echo $line;
> > }
> > $line = fgets($fh);
> > }
> > fclose ($fh);
> > }
> >>
> >
> > where $corpo is a variable for the name of the file.
> > if the file not existe the page have a blank body
> >
> > Thanks
> >
> > _________________________________
> > Gaetano Savoca
> > http://gsweb.altervista.org
> >
> >
> >
> >
> >
> > On Thursday, January 13, 2005 11:16 PM [GMT+1=CET],
> > Leif Gregory <Leifg@doh.state.nm.us> ha scritto:
> >
> >> Hello Gaetano,
> >>
> >> Thursday, January 13, 2005, 11:56:41 AM, you wrote:
> >>> How i can include in a HTML (with .php extension) file another HTML
> >>> file, i mean not all teh page but only the element of the <BODY>
> >>> tag?
> >>
> >>> I would to include with the include (nomefile); the body of other
> >>> page
> >>
> >>> There are some php function or some other way to do this?
> >>
> >> Uhmmm.. Lesse. I did something similar to this when I wrote some
> code
> >> to give me the headers from webservers. I had to use regexps to do
> >> it.
> >>
> >> Basically you could do something like this:
> >>
> >> <?php
> >>
> >> //Creating the filehandle for our input file
> >> $fh=fopen("myIncludeFile.html", "r");
> >>
> >> //Seeding the while loop below with the first line of the input file
> >> $line=fgets($fh);
> >>
> >> while (!feof($fh))
> >> {
> >> $line = fgets($fh);
> >>
> >> //Once we hit the <BODY> tag, start echoing out lines until we hit
> >> </BODY> if (preg_match("/<BODY/i", $line))
> >> {
> >> while (!preg_match("/<\/BODY/i", $line))
> >> {
> >> echo $line;
> >> $line = fgets($fh);
> >> }
> >> }
> >> else
> >> {
> >> $line = fgets($fh);
> >> }
> >> }
> >> fclose ($fh);
> >>
> >>>
> >>
> >>
> >> I didn't actually test this, but it should work. I just took some
> >> odds and ends pieces from something else I wrote that was similar.
> >>
> >> There's room for improvement. Like right now it'll continue
> >> processing the outer while loop after the </BODY> but since </HTML>
> >> usually follows on the next line I didn't think it too inefficient.
> >> You could make the outer while stop by ANDing with the condition of
> >> the inner while loop.
> >>
> >> As always... There's going to be other ways to do this. Some of them
> >> are probably a heck of a lot better than mine.
>
> --
> PHP Windows Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>