Solved

PHP - extract character range from text file

Posted on 2011-09-23
6
275 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 110

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 110

Expert Comment

by:Ray Paseur
ID: 36594002
0
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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 110

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

724 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