reading an html file into a string

This is a discussion on reading an html file into a string within the PHP Language forums, part of the PHP Programming Forums category; I'm trying to read an html file from my local server into a string, I'm using the following ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-30-2004
Alex Hopson
 
Posts: n/a
Default 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_contents.

Thanks
Alex



Reply With Quote
  #2 (permalink)  
Old 11-30-2004
Hilarion
 
Posts: n/a
Default 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


Reply With Quote
  #3 (permalink)  
Old 11-30-2004
Kelv
 
Posts: n/a
Default Re: reading an html file into a string

Alex Hopson wrote:

> I'm trying to read an html file from my local server into a string


$html = implode(file("yourfile.html"));

Hope that helps,

Kelv :)

--
LoHost
http://www.lohost.com
Reply With Quote
  #4 (permalink)  
Old 11-30-2004
Pedro Graca
 
Posts: n/a
Default Re: reading an html file into a string

["Followup-To:" header set to comp.lang.php.]
Alex Hopson wrote:
> 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;
> }

<snip>
> 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_contents.


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!
Reply With Quote
  #5 (permalink)  
Old 12-01-2004
Chung Leong
 
Posts: n/a
Default Re: reading an html file into a string

"Alex Hopson" <alex*under_score*hopson@hotmail.com> wrote in message
news:Of2rd.295$246.44@fe53.usenetserver.com...
> 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_contents.
>
> Thanks
> Alex


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_contents():

function file_get_contents($path) {
return fread(fopen($path, "rb"), 2147483647);
}


Reply With Quote
  #6 (permalink)  
Old 12-01-2004
Gary L. Burnore
 
Posts: n/a
Default Re: reading an html file into a string

On Tue, 30 Nov 2004 17:45:37 -0000, "Alex Hopson"
<alex*under_score*hopson@hotmail.com> wrote:

>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_contents.




<?php
function readinfile($template_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("filename.html");

and of course, you can use a variable:

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

$html = readinfile($source);



--
gburnore@databasix 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
================================================== =========================
Reply With Quote
  #7 (permalink)  
Old 12-01-2004
striver-no@spam-free.fr
 
Posts: n/a
Default Re: reading an html file into a string

Do something like that :

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


Pedro Graca wrote:
> ["Followup-To:" header set to comp.lang.php.]
> Alex Hopson wrote:
>
>>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;
>> }

>
> <snip>
>
>>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_contents.

>
>
> What happens when you do
>
> $fcontent = implode('', file($attfile));
>
> instead?

Reply With Quote
  #8 (permalink)  
Old 12-02-2004
d
 
Posts: n/a
Default 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($file);

:)

"Alex Hopson" <alex*under_score*hopson@hotmail.com> wrote in message
news:Of2rd.295$246.44@fe53.usenetserver.com...
> 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_contents.
>
> Thanks
> Alex
>
>



Reply With Quote
  #9 (permalink)  
Old 12-02-2004
Gary L. Burnore
 
Posts: n/a
Default Re: reading an html file into a string

On Thu, 02 Dec 2004 21:28:56 GMT, "d" <d@example.com> wrote:

>Doesn't anyone read the manual these days?


Must you top post?
--
gburnore@databasix 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
================================================== =========================
Reply With Quote
  #10 (permalink)  
Old 12-02-2004
d
 
Posts: n/a
Default Re: reading an html file into a string

oh you're one of those.

"Gary L. Burnore" <gburnore@databasix.com> wrote in message
news:coo1o9$ole$1@blackhelicopter.databasix.com...
> On Thu, 02 Dec 2004 21:28:56 GMT, "d" <d@example.com> wrote:
>
>>Doesn't anyone read the manual these days?

>
> Must you top post?
> --
> gburnore@databasix 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
> ================================================== =========================



Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 08:21 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0