We have a a supplier that each Monday sends a list of new Devices/Parts these are then manually entered into a database.
I'd like to use PHP to read this file and update the appropriate datebase records. The database I can do, but how do I read the data from the file ?
System: Avro
Supplier: ABC Inc
Quantity: 1
Device: ICD
ID: PA-658_ao8uY
For Clarity: PA-658_AO8UY
Quantity: 10
Device: PSTHG
ID: tg675_0O09i8
For Clarity: TG675_0O09I8
The above is an example of what we get. The System is us, the supplier is them. There can be hundreds for Quantity, device ID, and clarity lines in the file we receive.
How do I take the system / supplier names to variables, then loop through each of the Quantity, Device, ID and Clarity entries ???
For this simple task you don't need regular expressions, the following code will do it.
$content = file_get_contents("file.txt");
$content = str_replace(Array("\r\n", "\r"), "\n", $content); // we only want unix linebreaks
$data = explode("\n\n", $content);
foreach($data as &$section) {
$lines = explode("\n", $section);
$section = Array();
foreach($lines as $line) {
$colon = strpos($line, ":");
$section[substr($line, 0, $colon)] = trim(substr($line, $colon + 1));
}
}
print_r($data);
Sample Output Data:
Array
(
[0] => Array
(
[System] => Avro
[Supplier] => ABC Inc
)
[1] => Array
(
[Quantity] => 1
[Device] => ICD
[ID] => PA-658_ao8uY
[For Clarity] => PA-658_AO8UY
)
[2] => Array
(
[Quantity] => 10
[Device] => PSTHG
[ID] => tg675_0O09i8
[For Clarity] => TG675_0O09I8
)
)