473,326 Members | 2,255 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

treat large string like file

So, I've got some PHP code that reads in a file and parses some data as
it goes along.
However, now I'm thinking it might be easier for me to store chunks of
the file in the database rather than all the bits of data, so that it
can be interpreted in different ways if need be. But the problem is,
now I can't just use fread to read in two bytes of data, because I've
already got the whole chunk stored in a variable. And I really don't
want to deal with tons of substrings, it would be much easier to have a
sort of file pointer. So is there any way, or any set of functions
that I can use to deal with this?

Nov 24 '06 #1
4 1684
To get the data two chars at a time, you could do:

$len = strlen($data);
for ($i = 0; $i < $len; $i += 2) {
$str = substr($data, $i, 2);
echo "String is \"$str\".\n";
}

To access using a streams interface, you could dump the data into a
temporary file, then read it in. Or create a stream wrapper with
stream_wrapper_register.

Mark wrote:
So, I've got some PHP code that reads in a file and parses some data as
it goes along.
However, now I'm thinking it might be easier for me to store chunks of
the file in the database rather than all the bits of data, so that it
can be interpreted in different ways if need be. But the problem is,
now I can't just use fread to read in two bytes of data, because I've
already got the whole chunk stored in a variable. And I really don't
want to deal with tons of substrings, it would be much easier to have a
sort of file pointer. So is there any way, or any set of functions
that I can use to deal with this?
Nov 24 '06 #2

petersprc wrote:
To get the data two chars at a time, you could do:

$len = strlen($data);
for ($i = 0; $i < $len; $i += 2) {
$str = substr($data, $i, 2);
echo "String is \"$str\".\n";
}

To access using a streams interface, you could dump the data into a
temporary file, then read it in. Or create a stream wrapper with
stream_wrapper_register.
Well, the two characters at a time thing doesn't work very well,
because it's not always just two characters, and I've been doing a lot
of fseek-ing. The temp file solution could work, but that just sounds
like a horrible thing to do. Thanks anyway.

Nov 24 '06 #3
Mark wrote:
So, I've got some PHP code that reads in a file and parses some data as
it goes along.
However, now I'm thinking it might be easier for me to store chunks of
the file in the database rather than all the bits of data, so that it
can be interpreted in different ways if need be. But the problem is,
now I can't just use fread to read in two bytes of data, because I've
already got the whole chunk stored in a variable. And I really don't
want to deal with tons of substrings, it would be much easier to have a
sort of file pointer. So is there any way, or any set of functions
that I can use to deal with this?
Index into the whole chunk?

$two_chars = substr($whole_chunk, 17655, 2);
switch ($two_chars) {
case 'C3': $three_chars = substr($whole_chunk, 17657, 3);
break;
case '--': $three_chars = '---';
break;
default: $three_chars = substr($whole_chunk, 17655, 3);
break;
}

--
I (almost) never check the dodgeit address.
If you *really* need to mail me, use the address in the Reply-To
header with a message in *plain* *text* *without* *attachments*.
Nov 24 '06 #4

Pedro Graca wrote:
Mark wrote:
So, I've got some PHP code that reads in a file and parses some data as
it goes along.
However, now I'm thinking it might be easier for me to store chunks of
the file in the database rather than all the bits of data, so that it
can be interpreted in different ways if need be. But the problem is,
now I can't just use fread to read in two bytes of data, because I've
already got the whole chunk stored in a variable. And I really don't
want to deal with tons of substrings, it would be much easier to have a
sort of file pointer. So is there any way, or any set of functions
that I can use to deal with this?

Index into the whole chunk?

$two_chars = substr($whole_chunk, 17655, 2);
switch ($two_chars) {
case 'C3': $three_chars = substr($whole_chunk, 17657, 3);
break;
case '--': $three_chars = '---';
break;
default: $three_chars = substr($whole_chunk, 17655, 3);
break;
}

--
I (almost) never check the dodgeit address.
If you *really* need to mail me, use the address in the Reply-To
header with a message in *plain* *text* *without* *attachments*.
Err.. I think the best solution would be for me to make some sort of
wrapper class. Which deals with all the substr-ing and returns
whatever I ask for... so I can set up a function like

sseek($my_string_chunk_object,2); // string seek

which returns 2 (or however many) characters from the string and
advances the internal pointer.. (which would probably just an integer
to be used in the substr function)

then I could set up wrappers for how the data should be interpreted too
while I'm at it...

Nov 25 '06 #5

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

Similar topics

2
by: ohaya | last post by:
Hi, I'm a real newbie, but have been asked to try to fix a problem in one of our JSP pages that is suppose to read in a text file and display it. From my testing thus far, it appears this page...
6
by: guillaume | last post by:
I have to read and process a large ASCII file containing a mesh : a list of points and triangles. The file is 100 MBytes. I first tried to do it in memory but I think I am running out of memory...
12
by: Martin Dieringer | last post by:
I am trying to split a file by a fixed string. The file is too large to just read it into a string and split this. I could probably use a lexer but there maybe anything more simple? thanks m.
1
by: Mark | last post by:
Hi, I've seen some postings on this but not exactly relating to this posting. I'm reading in a large mail message as a string. In the string is an xml attachment that I need to parse out and...
16
by: Claudio Grondi | last post by:
What started as a simple test if it is better to load uncompressed data directly from the harddisk or load compressed data and uncompress it (Windows XP SP 2, Pentium4 3.0 GHz system with 3 GByte...
6
by: Lenny Wintfeld | last post by:
Hi I'm attempting additions/changes to a Java program that (among other things) uses XSLT to transform a large (96 Mb) XML file. It runs fine on small XML files but generates OutOfMemory...
13
by: liujiaping | last post by:
Hi, all. I have a dictionary-like file which has the following format: first 4 column 7 is 9 a 23 word 134 .... Every line has two columns....
2
by: Kevin Ar18 | last post by:
I posted this on the forum, but nobody seems to know the solution: http://python-forum.org/py/viewtopic.php?t=5230 I have a zip file that is several GB in size, and one of the files inside of it...
17
by: byte8bits | last post by:
How does C++ safely open and read very large files? For example, say I have 1GB of physical memory and I open a 4GB file and attempt to read it like so: #include <iostream> #include <fstream>...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.