[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

PHP - extract character range from text file

Posted on 2011-09-23
6
Medium Priority
?
285 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 111

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 2000 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 111

Expert Comment

by:Ray Paseur
ID: 36594002
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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 111

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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 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 …
Suggested Courses
Course of the Month20 days, 14 hours left to enroll

810 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