Solved

PHP - extract character range from text file

Posted on 2011-09-23
6
258 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 108

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 74

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 108

Expert Comment

by:Ray Paseur
ID: 36594002
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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 108

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

706 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now