reading an html file into a string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Hopson

    reading an html file into a string

    I'm trying to read an html file from my local server into a string, I'm
    using the following code:

    $attfile = $attachment[$i]; //create filenames
    $file_name = basename ($attfile);
    $lines = file($attfile); //get file into array

    foreach ($lines as $line_num => $line) { //concatenate each line
    $fcontent.= $line;
    }

    This should just load the file as an array then go through each line and add
    it to the end of the $fcontent variable. It does this but on larger files
    (4000+ lines of html) it stops part way through a line ver close to the end.
    Basically it misses off about 10-15 lines at the end of the html file

    I've increased the size of the file and it always seems to stop near the
    end, but the line where it quit before is finished. So I guess it's not some
    random character combination excaping it or reaching some kind of buffer
    limit.

    Does anyone have any ideas or know a simple script that will grab the
    contents of an html file and put it into a string (not an array). I'm using
    4.2.2 with no chance of upgrading so I can't use file_get_conten ts.

    Thanks
    Alex



  • Hilarion

    #2
    Re: reading an html file into a string

    Try this (instead of "foreach" loop):

    $fcontent = implode( '', $lines );

    You can also use "readfile" in combination with output buffering functions.


    Hilarion


    Comment

    • Kelv

      #3
      Re: reading an html file into a string

      Alex Hopson wrote:
      [color=blue]
      > I'm trying to read an html file from my local server into a string[/color]

      $html = implode(file("y ourfile.html")) ;

      Hope that helps,

      Kelv :)

      --
      LoHost
      LoHost - Reliable low cost web hosting with PHP4 and PHP5, MySQL 4 database, htaccess creator and Zend Optimizer. Secure email hosting with authenticated SMTP, IMAP email hosting, webmail, spam blocker, virus blocker, subdomains and vacation. UK hosting, USA hosting, secure hosting, cart hosting, blog hosting, nightly backups, raid servers and more.

      Comment

      • Pedro Graca

        #4
        Re: reading an html file into a string

        ["Followup-To:" header set to comp.lang.php.]
        Alex Hopson wrote:[color=blue]
        > I'm trying to read an html file from my local server into a string, I'm
        > using the following code:
        >
        > $attfile = $attachment[$i]; //create filenames
        > $file_name = basename ($attfile);
        > $lines = file($attfile); //get file into array
        >
        > foreach ($lines as $line_num => $line) { //concatenate each line
        > $fcontent.= $line;
        > }[/color]
        <snip>[color=blue]
        > Does anyone have any ideas or know a simple script that will grab the
        > contents of an html file and put it into a string (not an array). I'm using
        > 4.2.2 with no chance of upgrading so I can't use file_get_conten ts.[/color]

        What happens when you do

        $fcontent = implode('', file($attfile)) ;

        instead?
        --
        Mail to my "From:" address is readable by all at http://www.dodgeit.com/
        == ** ## !! ------------------------------------------------ !! ## ** ==
        TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
        may bypass my spam filter. If it does, I may reply from another address!

        Comment

        • Chung Leong

          #5
          Re: reading an html file into a string

          "Alex Hopson" <alex*under_sco re*hopson@hotma il.com> wrote in message
          news:Of2rd.295$ 246.44@fe53.use netserver.com.. .[color=blue]
          > I'm trying to read an html file from my local server into a string, I'm
          > using the following code:
          >
          > $attfile = $attachment[$i]; //create filenames
          > $file_name = basename ($attfile);
          > $lines = file($attfile); //get file into array
          >
          > foreach ($lines as $line_num => $line) { //concatenate each line
          > $fcontent.= $line;
          > }
          >
          > This should just load the file as an array then go through each line and[/color]
          add[color=blue]
          > it to the end of the $fcontent variable. It does this but on larger files
          > (4000+ lines of html) it stops part way through a line ver close to the[/color]
          end.[color=blue]
          > Basically it misses off about 10-15 lines at the end of the html file
          >
          > I've increased the size of the file and it always seems to stop near the
          > end, but the line where it quit before is finished. So I guess it's not[/color]
          some[color=blue]
          > random character combination excaping it or reaching some kind of buffer
          > limit.
          >
          > Does anyone have any ideas or know a simple script that will grab the
          > contents of an html file and put it into a string (not an array). I'm[/color]
          using[color=blue]
          > 4.2.2 with no chance of upgrading so I can't use file_get_conten ts.
          >
          > Thanks
          > Alex[/color]

          Don't see anything obvious. It could be a memory issue. Doing string
          concatention inside a loop has a way of eating up memory (because each
          iteration require the allocation of a bigger buffer). The simplest solution
          is to write your own file_get_conten ts():

          function file_get_conten ts($path) {
          return fread(fopen($pa th, "rb"), 2147483647);
          }


          Comment

          • Gary L. Burnore

            #6
            Re: reading an html file into a string

            On Tue, 30 Nov 2004 17:45:37 -0000, "Alex Hopson"
            <alex*under_sco re*hopson@hotma il.com> wrote:
            [color=blue]
            >I'm trying to read an html file from my local server into a string, I'm
            >using the following code:
            >
            > $attfile = $attachment[$i]; //create filenames
            > $file_name = basename ($attfile);
            > $lines = file($attfile); //get file into array
            >
            > foreach ($lines as $line_num => $line) { //concatenate each line
            > $fcontent.= $line;
            > }
            >
            >This should just load the file as an array then go through each line and add
            >it to the end of the $fcontent variable. It does this but on larger files
            >(4000+ lines of html) it stops part way through a line ver close to the end.
            >Basically it misses off about 10-15 lines at the end of the html file
            >
            >I've increased the size of the file and it always seems to stop near the
            >end, but the line where it quit before is finished. So I guess it's not some
            >random character combination excaping it or reaching some kind of buffer
            >limit.
            >
            >Does anyone have any ideas or know a simple script that will grab the
            >contents of an html file and put it into a string (not an array). I'm using
            >4.2.2 with no chance of upgrading so I can't use file_get_conten ts.[/color]



            <?php
            function readinfile($tem plate_file) {
            # Variable Declaration
            $i;
            $output;

            # Read file into temporary array
            $temp = file($template_ file);

            # Construct template output
            for ($i=0; $i<count($temp) ; $i++) {
            $output .= $temp[$i];
            }

            # Return results
            return $output;
            } // End readinfile
            ?>


            Then:

            $html = readinfile("/whatever/your/file/path/is/filename.html") ;
            or
            $html = readinfile("fil ename.html");

            and of course, you can use a variable:

            $source = "/whatever/your/file/path/is/filename.html";

            $html = readinfile($sou rce);



            --
            gburnore@databa six dot com
            ---------------------------------------------------------------------------
            How you look depends on where you go.
            ---------------------------------------------------------------------------
            Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
            | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
            DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
            | ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³
            Black Helicopter Repair Svcs Division | Official Proof of Purchase
            =============== =============== =============== =============== ===============
            Want one? GET one! http://signup.databasix.com
            =============== =============== =============== =============== ===============

            Comment

            • striver-no@spam-free.fr

              #7
              Re: reading an html file into a string

              Do something like that :

              $fp = fopen($my_file, "r");
              $f_content = fread($fp,files ize($my_file));
              fclose($fp);


              Pedro Graca wrote:[color=blue]
              > ["Followup-To:" header set to comp.lang.php.]
              > Alex Hopson wrote:
              >[color=green]
              >>I'm trying to read an html file from my local server into a string, I'm
              >>using the following code:
              >>
              >> $attfile = $attachment[$i]; //create filenames
              >> $file_name = basename ($attfile);
              >> $lines = file($attfile); //get file into array
              >>
              >> foreach ($lines as $line_num => $line) { //concatenate each line
              >> $fcontent.= $line;
              >> }[/color]
              >
              > <snip>
              >[color=green]
              >>Does anyone have any ideas or know a simple script that will grab the
              >>contents of an html file and put it into a string (not an array). I'm using
              >>4.2.2 with no chance of upgrading so I can't use file_get_conten ts.[/color]
              >
              >
              > What happens when you do
              >
              > $fcontent = implode('', file($attfile)) ;
              >
              > instead?[/color]

              Comment

              • d

                #8
                Re: reading an html file into a string

                Doesn't anyone read the manual these days? PHP has a function for doing
                just that.

                $fcontent=file_ get_contents($f ile);

                :)

                "Alex Hopson" <alex*under_sco re*hopson@hotma il.com> wrote in message
                news:Of2rd.295$ 246.44@fe53.use netserver.com.. .[color=blue]
                > I'm trying to read an html file from my local server into a string, I'm
                > using the following code:
                >
                > $attfile = $attachment[$i]; //create filenames
                > $file_name = basename ($attfile);
                > $lines = file($attfile); //get file into array
                >
                > foreach ($lines as $line_num => $line) { //concatenate each line
                > $fcontent.= $line;
                > }
                >
                > This should just load the file as an array then go through each line and
                > add it to the end of the $fcontent variable. It does this but on larger
                > files (4000+ lines of html) it stops part way through a line ver close to
                > the end. Basically it misses off about 10-15 lines at the end of the html
                > file
                >
                > I've increased the size of the file and it always seems to stop near the
                > end, but the line where it quit before is finished. So I guess it's not
                > some random character combination excaping it or reaching some kind of
                > buffer limit.
                >
                > Does anyone have any ideas or know a simple script that will grab the
                > contents of an html file and put it into a string (not an array). I'm
                > using 4.2.2 with no chance of upgrading so I can't use file_get_conten ts.
                >
                > Thanks
                > Alex
                >
                >[/color]


                Comment

                • Gary L. Burnore

                  #9
                  Re: reading an html file into a string

                  On Thu, 02 Dec 2004 21:28:56 GMT, "d" <d@example.co m> wrote:
                  [color=blue]
                  >Doesn't anyone read the manual these days?[/color]

                  Must you top post?
                  --
                  gburnore@databa six dot com
                  ---------------------------------------------------------------------------
                  How you look depends on where you go.
                  ---------------------------------------------------------------------------
                  Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
                  | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
                  DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
                  | ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³
                  Black Helicopter Repair Svcs Division | Official Proof of Purchase
                  =============== =============== =============== =============== ===============
                  Want one? GET one! http://signup.databasix.com
                  =============== =============== =============== =============== ===============

                  Comment

                  • d

                    #10
                    Re: reading an html file into a string

                    oh you're one of those.

                    "Gary L. Burnore" <gburnore@datab asix.com> wrote in message
                    news:coo1o9$ole $1@blackhelicop ter.databasix.c om...[color=blue]
                    > On Thu, 02 Dec 2004 21:28:56 GMT, "d" <d@example.co m> wrote:
                    >[color=green]
                    >>Doesn't anyone read the manual these days?[/color]
                    >
                    > Must you top post?
                    > --
                    > gburnore@databa six dot com
                    > ---------------------------------------------------------------------------
                    > How you look depends on where you go.
                    > ---------------------------------------------------------------------------
                    > Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
                    > | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
                    > DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
                    > | ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³
                    > Black Helicopter Repair Svcs Division | Official Proof of Purchase
                    > =============== =============== =============== =============== ===============
                    > Want one? GET one! http://signup.databasix.com
                    > =============== =============== =============== =============== ===============[/color]


                    Comment

                    • Wouter

                      #11
                      Re: reading an html file into a string

                      : --
                      : gburnore@databa six dot com
                      : ---------------------------------------------------------------------------
                      : How you look depends on where you go.
                      : ---------------------------------------------------------------------------
                      : Gary L. Burnore | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
                      : | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
                      : DataBasix | ÝÛ³ºÝ³Þ³ºÝ³³ÝÛº ݳ޳ºÝ³Ý³Þ³ºÝ³Ý ÝÛ³
                      : | ÝÛ³ 3 4 1 4 2 ݳ޳ 6 9 0 6 9 ÝÛ³
                      : Black Helicopter Repair Svcs Division | Official Proof of Purchase
                      :
                      =============== =============== =============== =============== ===============
                      : Want one? GET one! http://signup.databasix.com
                      :
                      =============== =============== =============== =============== ===============


                      WHY? is this so long?
                      waste of space..


                      Comment

                      Working...