473,785 Members | 2,468 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jul 17 '05 #1
10 2509
Try this (instead of "foreach" loop):

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

You can also use "readfile" in combination with output buffering functions.
Hilarion
Jul 17 '05 #2
Alex Hopson wrote:
I'm trying to read an html file from my local server into a string


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

Hope that helps,

Kelv :)

--
LoHost
http://www.lohost.com
Jul 17 '05 #3
["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_conten ts.


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!
Jul 17 '05 #4
"Alex Hopson" <al************ *********@hotma il.com> wrote in message
news:Of******** ******@fe53.use netserver.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_conten ts.

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_conten ts():

function file_get_conten ts($path) {
return fread(fopen($pa th, "rb"), 2147483647);
}
Jul 17 '05 #5
On Tue, 30 Nov 2004 17:45:37 -0000, "Alex Hopson"
<al************ *********@hotma il.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_conten ts.


<?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
=============== =============== =============== =============== ===============
Jul 17 '05 #6
Do something like that :

$fp = fopen($my_file, "r");
$f_content = fread($fp,files ize($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_conten ts.

What happens when you do

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

instead?

Jul 17 '05 #7
d
Doesn't anyone read the manual these days? PHP has a function for doing
just that.

$fcontent=file_ get_contents($f ile);

:)

"Alex Hopson" <al************ *********@hotma il.com> wrote in message
news:Of******** ******@fe53.use netserver.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_conten ts.

Thanks
Alex

Jul 17 '05 #8
On Thu, 02 Dec 2004 21:28:56 GMT, "d" <d@example.co m> wrote:
Doesn't anyone read the manual these days?


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
=============== =============== =============== =============== ===============
Jul 17 '05 #9
d
oh you're one of those.

"Gary L. Burnore" <gb******@datab asix.com> wrote in message
news:co******** **@blackhelicop ter.databasix.c om...
On Thu, 02 Dec 2004 21:28:56 GMT, "d" <d@example.co m> wrote:
Doesn't anyone read the manual these days?


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
=============== =============== =============== =============== ===============

Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
14673
by: Santah | last post by:
hi I'm new to C++ and I'm currently working on Visual C++ 6.0 I'm trying to open a text file, and read some data from it part of the text file looks like this: --------
3
3402
by: muser | last post by:
With the following code I'm trying to read a text file (infile) and output inaccuracies to the error file (printerfile). The text file is written and stored on disk, while the printerfile has to be created when the program executes. But the compile keeps reading that it can't find the text file. Karl you wrote the original program from which this one is but a poor copy, can you or anyone else enlighten me as to why yours worked and mine...
7
10582
by: jeff | last post by:
Hi. I am trying to read addresses from an Exchange 5.5 Global Address Book from an asp.net page. When the page executes the error "Collaboration Data Objects - ]" is generated. Here is the code: Dim strProfileInfo As String Dim ObjSession As New MAPI.Session
18
2244
by: Michael | last post by:
Hi, I moved to c++ from c, and wanted to know what the best way to read data from files is in c++. Any thoughts? fscanf() is possible but fairly painful! Regards Michael
19
10380
by: Lionel B | last post by:
Greetings, I need to read (unformatted text) from stdin up to EOF into a char buffer; of course I cannot allocate my buffer until I know how much text is available, and I do not know how much text is available until I have read it... which seems to imply that multiple reads of the input stream will be inevitable. Now I can correctly find the number of characters available by: |
3
3637
by: dale zhang | last post by:
Hi, I am trying to read an image from MS Access DB based on the following article: http://www.vbdotnetheaven.com/Code/Sept2003/2175.asp The article author is using PictureBox for windows application, while I am doing for web. I can only find Image from web forms control and HTML control. This may be the root cause of my problem. For read button, I converted his VB to the C#. But the compiler complains:
3
1887
by: Luqman | last post by:
How can I retrieve text from html file and write to textbox. Best Regards, Luqman
1
2117
by: Chandra | last post by:
Hi, I have a problem related to File Reading. I have a file which contains data which is in Hexa format. I have to read that data and push it to a function which takes a parameter of type char. I am able to read the data from the file, which i am getting as characters. For example: Suppose the Hexa value from the File data is ABCDEFGH. In hexa form 0xAB is one byte. So, while reading this data A is taken as a byte which is not a...
4
2150
by: radhikams | last post by:
hi I have written a code for writing the data of a column from database into a file and again reading that file and displaying. Im writing into an .html file....Now the problem is im getting the output but that output is followed by an error..... This is the output ----------------------------- A large number of processes are available, and can be applied to an input or a group of inputs. These processes can be chained...
0
9491
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10357
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4063
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.