Jump to content


Photo

Reading Segments of a Text File


  • Please log in to reply
5 replies to this topic

#1 check_it_out

check_it_out
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 12 April 2006 - 05:24 PM

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

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 12 April 2006 - 06:24 PM

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.

Info: PHP Manual


#3 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 12 April 2006 - 06:35 PM

[!--quoteo(post=364148:date=Apr 12 2006, 02:24 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Apr 12 2006, 02:24 PM) View Post[/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.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#4 check_it_out

check_it_out
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 12 April 2006 - 06:48 PM

[!--quoteo(post=364148:date=Apr 12 2006, 02:24 PM:name=ober)--][div class=\'quotetop\']QUOTE(ober @ Apr 12 2006, 02:24 PM) View Post[/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) View Post[/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.


#5 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 12 April 2006 - 06:51 PM

[!--quoteo(post=364154:date=Apr 12 2006, 02:46 PM:name=dapper)--][div class=\'quotetop\']QUOTE(dapper @ Apr 12 2006, 02:46 PM) View Post[/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:

$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";

hope this helps
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#6 check_it_out

check_it_out
  • New Members
  • Pip
  • Newbie
  • 3 posts

Posted 12 April 2006 - 07:26 PM

[!--quoteo(post=364155:date=Apr 12 2006, 02:51 PM:name=obsidian)--][div class=\'quotetop\']QUOTE(obsidian @ Apr 12 2006, 02:51 PM) View Post[/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:

$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";

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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users