阅读文本文件

时间:2013-02-11 09:36:03

标签: php flat-file

我们有一个供应商,每个星期一发送新设备/零件清单,然后手动输入数据库。

我想使用PHP来读取此文件并更新相应的数据库记录。 我可以做的数据库,但如何从文件中读取数据?

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

以上是我们得到的一个例子。系统是我们,供应商是他们。 我们收到的文件中有数百种数量,设备ID和清晰度行。

如何将系统/供应商名称转换为变量,然后遍历每个数量,设备,ID和清晰度条目???

3 个答案:

答案 0 :(得分:1)

对于这个简单的任务,您不需要正则表达式,以下代码将执行此操作。

$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);

示例输出数据:

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
        )

)

答案 1 :(得分:0)

如果您可以将此文件的格式重新定义为此类..

   [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和all。您可以为各种供应商创建多个NewStock对象。希望这有帮助

答案 2 :(得分:0)

    $handle = fopen("your file ");
    //counter
    $i=0;
    $rowContentPrec=array();
    while (!feof($handle)) {
     $rowContent=explode(':',fread($handle, 8192));
     //get the system and provider
      if($i==0){      
          $sytem=$rowContent[1];
      }elseif($i==1){
          $provider=$rowContent[1];  
      }elseif(isset($rowContent)){ 
          //product
          $rowContent=explode(':',fread($handle, 8192));
          if($rowContentPrec[0]=='For Clarity'){
               //save your product to database here and initialize the    array with informations about one product
                $contentArr=array();
          }else{
            $contentArr[$rowContent[0]]=$rowContent[1];  
          } 
           $rowContentPrec=$rowContent;
          $i++; 
      }

    }
    fclose($handle);