Data Parsing Assistance with data block lines that are uneven

Hi. I have a php file that parse a txt database well when the database block have equal number of characters per line. Recently, I was given a 1 million line database block that has 488 characters on most of the lines, but 508 characters on others. My current php parser file would pick up the extra characters from longer lines thus screwing up the parsed data. How do I (1) pad 20 white spaces to lines with 488 characters; or (2) strip 20 characters at the end of the line; or (3) tell my parser not to read any character on the same line beyond 488?

The attached test.txt is a small section of the database, and the testOut.txt reflected the output correctly until it hits the line with longer characters.

How do I (1) pad 20 white spaces to lines with 488 characters; or (2) strip 20 characters at the end of the line; or (3) tell my parser not to read any character on the same line beyond 488?

Which would you prefer to do?

You can pad output using str_pad()

That should be fine. How do I str_pad() by adding 20 whitespaces to 488-character lines, but skipping lines with 508 characters?

I realized fread() ignores end of line in the txt database. Is there another function I can use that will recognize end of line so fread() will skip 20 null spaces?

Str_pad sets the total width. So a string with 10 characters, and another with 20 will work when padded. It only adds the padding, if needed.

The current txt file is one long 1-million-line string.

This is for the parser, or are you wanting to keep the extremely long string?

Not at all. I used fread(). But certain lines have 488 chars and others 508. fread() just keeps going. I am trying to find a function or a method that will recognize the different line length.

I uploaded two files. One showing a section of the string file, and another showing the desired parsed output until I hit lines with longer characters.

fgets grabs data line by line.

Thanks. I’ll give that a try.

I take that back… You are dealing with a csv file?
[php]
$row = 1;
if (($handle = fopen(“testOut.txt”, “r”)) !== FALSE) {
while (($data = fgetcsv($handle, 1000, “,”)) !== FALSE) {
$num = count($data);
echo "

$num fields in line $row:

\n";
$row++;
for ($c=0; $c < $num; $c++) {
echo $data[$c] . “
\n”;
}
}
fclose($handle);
}[/php]

Also, is that live data? If they are real people, I will remove the files so that they are not publicly available.

Thanks for the advisement. Data is live. I could not delete them from my original post. I must keep that in mind next time I post.

It’s not a cvs file. What do you call a file that only has a set of chars like below?

jalskdfjklk231232 343 sext e23r4
klwlk 33mml 3234r 3234 weras r323
32ras fasfasdf rwr45fgse t3456345w4
32as r325as 4345w t45345 t345wtw3

fgets() is working. I just need to figure out where the extra characters come from.

Hmmm, I ran the posted file and it worked against the code sample I posted as a csv file type, using txt as the extension.

That is still a delimited file.

Thanks. fgets() worked!

Another quick question. I could cap the first letter if the city has only one name. But if city has 2 or 3 names, how do I get the first letter of second and third name capped?

$city_addr = ucfirst(strtolower(trim(substr($line, 159, 40))));

above yields “Breezy point west” when I need “Breezy Point West”

ucwords()

I removed the attached files for you as well.

Sponsor our Newsletter | Privacy Policy | Terms of Service