I have a set of textfiles containing personal data. I want to use a script to run through them and write them to a mysql database.
What I have done so far is to read the text file into a string, explode the string at ” ” and then run through the array to find the required information. There are a couple of problems with that though, so maybe someone knows a better solution alltogether.
The base text file looks like this:
Name: Marius
Address 1: Street
Address 2: Town
Address 3: Country
etc
The problem in this is that I have no way of knowing how long the ‘value’ of i.e. Street is. Could be just the ‘Somestreet’ or ‘Some Street’ or ‘Some Street 1337’. This goes for pretty much every field.
Is there maybe a method to turn that text into an array, where for every item the string ending with “:” is used as the key and every following item without ending “:” is considered the value?
As Adam hints in the comments: If there is no tab, cr/lf or similar delimiter, there is not enough information in your string to parse the data by.
If there is only a limited set of keys, you can provide this set as additional information.
Since you want to insert the data into a mysql table, I assume that the keys are limited and unique.
With an array of these key strings, your parser algorithm would work this way:
This should do the trick.