Solved

PHP - extract character range from text file

Posted on 2011-09-23
6
264 Views
Last Modified: 2012-05-12
Hi Experts,

I have a collection of structured text files in a folder.  Each file contains over 100 fields.  All the fields are fixed length (space delimited) so they appear in exactly the same position in each file.  What I want to do is write PHP a script that will allow me to read each file and grab 3 fields between certain character positions.  I need to write the values from each file to a text file (one line per file, comma separated between the fields).  

There are well over 5000 files in the folder so alternatively I can write this to a MySQL table if this is more practical?

Is this possible and how would I go about it?

Kind Regards,

John
0
Comment
Question by:john-formby
  • 3
  • 2
6 Comments
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 36590746
Can you please post some test data - perhaps three files.  And explain what the rules are for finding and extracting the 3 fields you want.  Thanks, ~Ray
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 36593961
Agreed. It sounds like you want to do a simple substring search, and regex might be overkill for this.
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 36594002
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 14

Author Comment

by:john-formby
ID: 36707816
Hi,

Sorry for the delay in replying, I have not been well for the past couple of days.

The only rules I have for the files is that I need to extract whatever is between certain character positions.  All the files are in the same format so it is a case of I need to remove the string that appears between:

Position 9 and 19 on line 1 (e.g. SO123456789)
Position 20 and 25 on line 1 (e.g. 010111)
Position 83 and 88 on line 1 (e.g. 020111)
Position 93 and 98 on line 1 (e.g. 030111)

I need to extract this for each file in the directory and either create a comma seperated text file (one file per line) or write to a MySQL database table.

Please can you have a look at the attached files and let me know if this is possible?

Kind Regards,

John

 files.zip
0
 
LVL 14

Author Comment

by:john-formby
ID: 36708179
Hi,

I never refreshed before replying so didn't see the other replies.  I have now managed to do this by using substr.  I open the files in the directory one by one, gran the strings I require and write them to a new text file.

Many thanks for your replies.

Kind Regards,

John
0
 
LVL 109

Expert Comment

by:Ray Paseur
ID: 36708585
Good morning, John.  I see you finally posted the test data at about 3:00am local time here in the USA and then closed the question before 4:30am.  Sorry I did not get a chance to help you, but most of the USA was asleep during that short window.  Best of luck, ~Ray
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

806 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question