如果您可以将此文件的格式重新定义为这样的..
[Core]
System=Avro
Supplier=ABC Inc
[line1]
Quantity= 1
Device=ICD
ID=PA-658_ao8uY
For Clarity=PA-658_AO8UY
[line2]
Quantity=10
Device=PSTHG
ID=tg675_0O09i8
For Clarity: TG675_0O09I8
你可以使用parse_ini_file(file,true,INI_SCANNER_NORMAL)
可以给你所有数据的多维数组。
这是您可以使用的另一种高度主观的解决方案。我只是假设格式是稳定的,并且会保持很长时间。
<?php
$newStock = new NewStockUpdate($itemListFile);
//do anything with $newStock Object
class NewStockUpdate
{
private $System;
private $Supplier;
private $allUpdates;
function __construct($listFile)
{
$fileHandle = fopen( $listFile, "r" ) or die("Couldn't open Update file $listFile");
$lineSystem = explode(":",getLineData($fileHandle));
$lineSupplier = explode(":",getLineData($fileHandle));
$i=0;
while(true)
{
$allUpdates[$i] = new ItemData($fileHandle);
$i++;
}
}
function getSystem()
{
return $this->System;
}
function getSupplier()
{
return $this->Supplier;
}
function getUpdateList()
{
return $this->allUpdates;
}
}
class ItemData
{
public $Quantity;
public $Device;
public $ID;
public $ForClarity;
public $lastObject;
function __construct($filePointer)
{
try
{
$lineQuantity = explode(":",getLineData($filePointer));
$lineDevice = explode(":",getLineData($filePointer));
$lineID = explode(":",getLineData($filePointer));
$lineForClarity = explode(":",getLineData($filePointer));
$this->Quantity = $lineQuantity[1];
$this->Device = $lineDevice[1];
$this->ID = $lineID[1];
$this->ForClarity = $lineForClarity[1];
}
catch(Exception $e)
{
//log something
}
if(feof($filePointer))
{
$this->lastObject = true;
}
else
{
$this->lastObject=false;
}
function isLastRecord()
{
return $this->lastObject;
}
}
}
function getLineData($filePointer)
{
while(!feof($filePointer))
{
$data = fgets($filePointer);
if(empty($data)|| $data=='\n')
{
$data = fgets($filePointer);
}
else
{
return $data;
}
}
}
?>
我认为其余的你可以用类对象来管理。将这些条目添加到 db 和所有。您可以为各种供应商创建多个 NewStock 对象。希望这会有所帮助