日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php安装dat,PHP Parsing a .dat file

發布時間:2023/12/1 php 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php安装dat,PHP Parsing a .dat file 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

問題

I have a .dat file that is essentially ; delimited file and I'm trying to convert it to a tab delimited .txt. The problem that I am not sure about is that each row of the new file will be a combination of 3 of the original file's rows, each original row has a different quantity of data. The first column just identifies each row in a grouping. What would be the best way to do this?

Sample original data:

01;zxc;asd;qwe;uio;jkl;asd;123;456

02;lkj;oiu;oji

03;fjifao;vbofekjf;fjieofk;aoijf;voien3984

01;lkj;oiu;fji;eoj;vnk;fji;098;321

02;fji;oje;jvi

03;jie;voi;djv;eojf;38723

End output:

zxc asd qwe uio jkl asd 123 456 lkj oiu oji fjifao vbofekjf fjieofk aoijf voien3984

lkj oiu fji eoj vnk fji 098 321 fji oje jvi jie voi djv eojf 38723

Any ideas?

回答1:

Here's how I'd do it:

$lines = file($data);

$rows = array();

$row_pivot = -1;

foreach ($lines as $line) {

// Split line by ;

$data = explode(';', trim($line));

// Get the first element

$r_id = array_shift($data);

if ($r_id == '01') {

// When 01 is the first element, start a new row

// You can dump the previous row here as well if you aim for speed

$row_pivot++;

$rows[$row_pivot] = array();

}

// Add data to row

$rows[$row_pivot] = array_merge($rows[$row_pivot], $data);

}

// Print rows

foreach ($rows as $r) {

echo implode("\t", $r)."\n";

}

回答2:

I would personally explode the data then foreach row in the resulting array, then again explode each line at your delimiter ';' and then format an output that is tab delimited.

$data = 'LOADED FILE DATA';

$lines = preg_split( '/\r\n|\r|\n/', $data);

$out = '';

foreach($lines as $line){

$parts = explode(';',$line);

foreach($parts as $part){

$out .= $part.'\t';

}

$out .= '\n';

}

echo $out;

?>

code untested.

回答3:

Should be something like this

$lines = file($filename);

$lineCount = count($lines);

$output = '';

for ($i = 0; $i < $lineCount - 2; $i += 3) {

$newLines = array();

for ($j = $i; $j < $i + 3; $j++) {

list($_, $rest) = explode(';', isset($lines[$j]) ? $lines[$j] : '');

$newLines = array_merge($newLines, $rest);

}

$output .= implode("\t", $newLines) . "\n";

}

來源:https://stackoverflow.com/questions/6657191/php-parsing-a-dat-file

總結

以上是生活随笔為你收集整理的php安装dat,PHP Parsing a .dat file的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。