Text file to array
Script for re-sale. I'd like to read a text file into an array so that I have the values available for placing into an HTML file. There are a few other requirements to help with error checking, documentation, and layout, but it's pretty straightforward. Please note that my PHP below may contain syntax errors, and you may be able to offer a better use of PHP syntax.
EXAMPLE
-----------------------------------------------
This is an example text data file
The first valid line has field names
Other valid lines have data
Code|Name|Description|Options|Cost
Dresses
DR1-5|Dress with straps|Very nice|Red,Blue,Green|10.00
DR1-6|Dress without straps|Even nicer|Red,Blue|9.00
DR3-3|Dress with long bits|Awsome number|Black only|20.00
Tops
Top-2|Red Sweater|Looks great|Red,Blue|£12.00
Top-2a|Red Sweater (large)|Looks greater|Red,Blue|14.00
-----------------------------------------------
And produces an array, for example, $products such that:
echo $products(0,'Code') // Displays 'Code'
echo $products(0,'Description') // Displays 'Descripton'
echo $products(1,'Code') // Displays 'DR1-5'
echo $products(1,'Name') // Displays 'Dress with straps'
echo $products(1,'Options') // Displays 'Red,Blue,Green'
// Can we do this?...
echo $products(1,'Options',1) // Displays 'Red'
echo $products(1,'Options',2) // Displays 'Blue'
echo $products(1,'Options',3) // Displays 'Green'
// How about this?...
echo $products(DR1-5,'Options',1) // Displays 'Red'
FEATURES
1. Read an ASCII text file with specified configurable delimiters respresenting records and fields. Eg. \n for end of line (end of record), and | for end of field.
2. The first valid records contains field names, subsequent valid names contain data.
3. To be documented, organised into functions, and include configuration options at the beginning (eg. record a field delimiters).
4. To optionally handled Comma Separated Values, eg. "DR1-5","Dress with straps","Very nice","Red,Blue,Green","10.00"
Note that the field separated may appear within the quotes!
5. The resulting array will ignore lines (ie remove invalid records) that do not have a complete record, and place the invalid lines into another array. Optionally displaying this array will allow a designer to quickly see whether the original text file is formatted correctly.
6. Functons that demonstrates working data and code:
.. a. A function to display the field names, eg.:
.. .. .. Code | Name | Description | Options | Cost
.. b. A function to display the contents of a specifed record, eg.:
.. .. .. DR1-5 | Dress with straps | Very nice | Red,Blue,Green | 10.00
.. c. Display the contents of all records where Title includes the text "Dress", eg:
.. .. .. DR1-5 | Dress with straps | Very nice | Red,Blue,Green | $10.00
.. .. .. DR1-6 | Dress without straps | Even nicer | Red,Blue | $9.00
.. .. .. DR3-3 | Dress with long bits | Awsome number | Black only | $20.00
Regards,
Ian