Jump to content

Archived

This topic is now archived and is closed to further replies.

check_it_out

Reading Segments of a Text File

Recommended Posts

I have a text file that I need to be able to parse line by line. The file is in an awkward format with lots of white space. Essentially the file contains names and addresses that I would like to extract into an Excel spreadsheet. Below is an example of one line of text in the file.


200520052032-16-0016 01985930000940117 20887354HX 0000016184400000025000000001368440200 5527650KELLY SYLVIA R 120 WHISPERING OAKS CT SARASOTA FL 34232

This is really one long line of text. This example when you see it will not contain all the white spaces and the amount of white space is not consistent from variable to variable.

The substring "00000161844" is the value of the homeowner's house, ie. $161,844.

Therefore I would like to extract the name, address, and home value from this line into a form that I could later put into an Excel spreadsheet. I am stumped by what string function to use given the variable white space in the line.

Thanks for any help,

Dapper

Share this post


Link to post
Share on other sites
Is the file, by chance seperated by tabs instead of spaces? If so, you can use whatever function you'd like to read in line by line (fgets for example) and then you can just use explode() to break the string up into its various pieces.

Share this post


Link to post
Share on other sites
[!--quoteo(post=364148:date=Apr 12 2006, 02:24 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Apr 12 2006, 02:24 PM) [snapback]364148[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Is the file, by chance seperated by tabs instead of spaces? If so, you can use whatever function you'd like to read in line by line (fgets for example) and then you can just use explode() to break the string up into its various pieces.
[/quote]


right, and if it's delimited by tabs or something like that, you could also use fgetcsv() to pull it all in for you.

Share this post


Link to post
Share on other sites
[!--quoteo(post=364148:date=Apr 12 2006, 02:24 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Apr 12 2006, 02:24 PM) [snapback]364148[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Is the file, by chance seperated by tabs instead of spaces? If so, you can use whatever function you'd like to read in line by line (fgets for example) and then you can just use explode() to break the string up into its various pieces.
[/quote]

Thanks for getting back to me. Unfortunately the readable characters are separated by different amounts of white space, sometimes 38 blank spaces, 75 blank spaces, etc.


[!--quoteo(post=364152:date=Apr 12 2006, 02:35 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ Apr 12 2006, 02:35 PM) [snapback]364152[/snapback][/div][div class=\'quotemain\'][!--quotec--]
right, and if it's delimited by tabs or something like that, you could also use fgetcsv() to pull it all in for you.
[/quote]


Thanks for getting back to me. Unfortunately the readable characters are separated by different amounts of white space, sometimes 38 blank spaces, 75 blank spaces, etc.

Share this post


Link to post
Share on other sites
[!--quoteo(post=364154:date=Apr 12 2006, 02:46 PM:name=dapper)--][div class=\'quotetop\']QUOTE(dapper @ Apr 12 2006, 02:46 PM) [snapback]364154[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Thanks for getting back to me. Unfortunately the readable characters are separated by different amounts of white space, sometimes 38 blank spaces, 75 blank spaces, etc.
[/quote]

here's an idea: just use a pattern match to pull all the words out of the string into an array, and then echo through the array:

[code]
$String = "200520052032-16-0016 01985930000940117 20887354HX 0000016184400000025000000001368440200 5527650KELLY SYLVIA R 120 WHISPERING OAKS CT SARASOTA FL 34232";

preg_match_all('|\b[^\s]+\b|ims', $String, $matches);

foreach ($matches[0] as $x)
    echo "$x<br />\n";
[/code]

hope this helps

Share this post


Link to post
Share on other sites
[!--quoteo(post=364155:date=Apr 12 2006, 02:51 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ Apr 12 2006, 02:51 PM) [snapback]364155[/snapback][/div][div class=\'quotemain\'][!--quotec--]
here's an idea: just use a pattern match to pull all the words out of the string into an array, and then echo through the array:

[code]
$String = "200520052032-16-0016 01985930000940117 20887354HX 0000016184400000025000000001368440200 5527650KELLY SYLVIA R 120 WHISPERING OAKS CT SARASOTA FL 34232";

preg_match_all('|\b[^\s]+\b|ims', $String, $matches);

foreach ($matches[0] as $x)
    echo "$x<br />\n";
[/code]

hope this helps
[/quote]
Thanks, I will give it a try.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.