what is the best way of extracting multiple (~40 values) from a text file using php?
the data is more or less like:
NAMEA valuea
NAMEB valueb
I’m looking for a proper* approach to extracting this data into a data-structure, because i will need to specify regexs for all of them (all 40).
did i make myself clear?
*meaning, the default/painful method would be for me to do:
$namea = extractfunction("regexa", $textfilevalue);
$nameb = extractfunction("regeb", $textfilevalue);
… 40 times!
The lines may not be in the same order, or be present in each file. Every NAMEA is text like: “Registration Number:”, or “Applicant Name:” (ie, with spaces in what i was calling as NAMEA)
Response to the Col.
i’m looking for a sensible “way” of writing my code, so its readable, modifiable, builds an object/array thats easily callable, etc… “good coding style!” 🙂
@Adam – They do actually… and contain slashes as well…
@Alix – Freaking marvelous man! THat was GOOD! would you also happen to have any insights on how I can “truncate” the rsultant array by removing everything from “key_x” and beyond? Should i open that as a new question?
Here is my take at it:
somefile.txt:
PHP Code:
Output:
You may also be able to further simplify this by using
str_getcsv()on PHP 5.3+.EDIT: My previous version fails for keys that have spaces like @Col. Shrapnel noticed. I didn’t read the question with enough attention. A possible solution since you seem to be using keys that always have
:appended is this:To remove everything from
key_xto the end of the file you can do something like this:So the whole thing would look like this:
somefile.txt:
PHP Code:
Output: