3

我有一个看起来像这样的文件:

1028806~HDR~20110815~15-AUG-2011~C~23:10~~~~~~~
1028806~DTL~C3914A~HWP-C3914A~1000949~A~LASERJET MAINT KIT 8100/N/DN~HEWLETT PACKARD~2659~12~0~0~475.75~658.75~0~3~Y~2~~2~475.75~5~~~009088336~~3179~10.60~N~8.25~8.50~20.50~~088698601976~44103109~6A~20030627~NNY~~A~S~~~~~~N~~~~~~20.50~8.50~8.25~~~~~~~~~~~~~~~~
1028806~DTL~70023301~OKI-70023301~1002121~A~OKILAN 6020E+ 10/100BASE-TX ETHERNET EXT~OKI PRINTING SOLUTIONS~2703~0~0~0~55.17~80.00~0~0~Y~0~~0~55.17~0~~~009117000~~2160~2.79~N~8.00~8.75~14.00~~000000180016~44101700~ACC-IMPACT~19950723~NNY~~A~S~~~~~~N~~~~~~14.00~8.75~8.00~~~~~~~~~~~~~~~~
1028806~DTL~PRO7T~APC-PRO7T~1003150~A~Professional-grade Protection for Computers and Electronics~AMERICAN POWER CONVERSION~20664~7~0~0~21.60~36.00~0~0~Y~0~~0~21.60~7~~~008112000~~4400~2.00~N~1.90~6.90~12.40~~731304000181~39121610~SURG~19950723~NNY~~A~S~~~~~~N~~~~~~12.40~6.90~1.90~~~~~~~~~~~~~~~~
1028806~DTL~PER7~APC-PER7~1003418~A~Surge suppressor ( external ) / 7 output connector(s)~AMERICAN POWER CONVERSION~20664~496~50~0~9.30~15.25~0~3~Y~86~~363~9.30~44~~~008118000~~4400~1.85~N~2.10~6.90~11.50~~731304000112~39121610~SURG~20011025~NNY~~A~S~~~~~~N~~~~~~11.50~6.90~2.10~~~~~~~~~~~~~~~~
1028806~DTL~PRO7~APC-PRO7~1003761~A~APC SurgeArrest Professional - Surge suppressor ( external ) - AC 120 V - 7 outp~AMERICAN POWER CONVERSION~20664~88~0~0~17.59~30.00~0~0~Y~12~~52~17.59~24~~~008112000~~4400~1.95~N~2.25~7.50~12.25~~731304000174~39121610~SURG~19950723~NNY~~A~S~~~~~~N~~~~~~12.25~7.50~2.25~~~~~~~~~~~~~~~~

我需要使用脚本来读取每一行的某些部分(粗体部分):

1028806~DTL~ C3914A ~HWP-C3914A~1000949~A~ LASERJET 维修套件 8100/N/DN ~HEWLETT PACKARD~2659~12~0~0~475.75~658.75~0~3~Y~2~~2~475.75 ~5~5~009088336~3179~10.60~N~8.25~8.50~20.50~088698601976~44103109~6A~20030627~NNY~~A~S~~~~N~~~~20.50~ 8.50~8.25~~~~~~~~~~~~~~~~

该文件有超过 30 万个项目,因此不能手动进行,所以当我不知道部分 # 和描述有多长时,如何让脚本只读取这些部分?同时忽略所有其他 ~ 字符。

谢谢

4

3 回答 3

4

fgetcsv()在这里可以提供帮助,比一次加载整个文件并将explode()所有行放入一个巨大的数组中更节省内存。

if (($handle = fopen("/path/to/file", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, "~")) !== FALSE) {
       echo $data[2] . " " . $data[6];
    }
}
fclose($handle);
于 2011-08-16T16:16:35.443 回答
1
// read the file
$lines = file('file.txt');

// loop through each line
foreach($lines as $line){
    // separate the parts by the ~ delimiter up to the second bold part
    // ignoring the rest of ~
    $parts = explode('~', $line, 7);
    echo $parts[2]; // output first bold part
    echo $parts[6]; // output second bold part
}
于 2011-08-16T16:16:16.077 回答
1

看起来你可以在波浪号上爆炸:

$fields = explode('~', $line);
$part_num = $fields[2];
$desc = $fields[6];
于 2011-08-16T16:17:07.943 回答