• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 287
  • Last Modified:

PHP - extract character range from text file

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
john-formby
Asked:
john-formby
  • 3
  • 2
1 Solution
 
Ray PaseurCommented:
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
 
käµfm³d 👽Commented:
Agreed. It sounds like you want to do a simple substring search, and regex might be overkill for this.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
john-formbyAuthor Commented:
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
 
john-formbyAuthor Commented:
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
 
Ray PaseurCommented:
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

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now