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?
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";
}
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.
<?php
$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.
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";
}
Related
I have a text file
(0:00)
text1
text2
(0:30)
text text text text text
..................
the result should be like this
array("(0:00)"=>"text1 text2","(0:30)"=>"text text text text text")
my code
$key = array();
$val = array();
$out = array();
$file = file('1.txt');
foreach($file as $line) {
$line = trim($line);
if (preg_match("/(\d{0,2}:\d\d)/",$line,$match)){
$key[]=$match;
}else{
$val[]=$match;
}
$out=array_merge($key,$val);
}
echo '<pre>';
print_r($out);
tell me, how to fix it?
Try this:
$n = preg_match_all('/\((\d{0,2}:\d\d)\)([\s+]*?)([^\(]*)/mi', $file, $matches);
$out = array();
for ($i = 0; $i < count($matches[1]); ++$i)
{
// for removing newline characters:
$out[$matches[1][$i]] = trim(preg_replace('/\s\s+/', ' ', $matches[3][$i]));
}
var_dump($out);
At first i tried it with the way you were going, but using one regex for both seemed to be much easier.
It gave me following result:
array(2) {
["0:00"]=>
string(11) "text1 text2"
["0:30"]=>
string(24) "text text text text text"
}
You can loop the array looking for the first key, and then add all occurences to this key until you find next.
Also, your code had several logical errors:
$key[]=$match; - $match is an array, you should use the first occurence
$val[]=$match; - $match is empty in this case (match wasn't found), you should use the $line
Corrected code:
$out = array();
$key = "";
$file = file('aaa.txt');
foreach($file as $line) {
$line = trim($line);
if(empty($line)) continue;
if (preg_match("/(\d{0,2}:\d\d)/",$line,$match)){
$key = $match[0];
continue;
}
if(!empty($key)){
$out[$key] = $line;
}
}
echo '<pre>';
print_r($out);
$text = file_get_contents('1.txt');
preg_match_all("~\(\d{1,2}:\d{1,2}\)~", $text, $keys);
$values = array_values(preg_grep("~.+~", preg_split("~\(\d{1,2}:\d{1,2}\)~", $text)));
$final = [];
for ($n = 0; $n < count($keys[0]); $n++) {
$final[$keys[0][$n]] = $values[$n];
}
print_r($final);
Got this code php
$data ='
one;uno
two;dos
three;tres
four;cuatro
'
I want to print the first column in a row, separating elements with comma and aspace, to obtain this result:
one, two, three, four
Any help please? I'm doing this but I can`t:
<?php
$data ='
one;uno
two;dos
three;tres
four;cuatro
';
$line = explode("\n", $data);
for($i = 0; $i<count($line); $i++) {
$item = explode(";", $line[$i]);
$coma = implode(', ', $item[0{);
echo $coma;
}
?>
try this modified version of your code
$data ='one;uno
two;dos
three;tres
four;cuatro';
$coma=array();
$line = explode("\n", $data);
for($i = 0; $i<count($line); $i++) {
$item = explode(";", $line[$i]);
$coma[]= $item[0];
}
echo implode(',',$coma);
I have text file
name,name1
willhaveishere1
name,name2
willhaveishere2
name,name3
willhaveishere3
i want read it and return like that
$nn = name1
$ss = willhaveishere1
with my code i get only name1
my code is
$file1 = "file.txt";
$file = file($file1);
$count = count($file);
if($count > 0) {
$i = 1;
foreach($file as $row) {
$n = strstr($row, 'name,');
$cc = array("name,");
$dd = array("");
$nn = str_replace($cc, $dd, $n);
echo $nn;
$i++; } }
This is probably what you need
if($count > 0) {
foreach($file as $row) {
$pos = strpos($row, ',');
if($pos !== false){
echo substr($row, $pos + 1);
$nn[] = substr($row, $pos + 1);
} else {
echo $row;
$ss[] = $row;
}
}
}
EDIT
Yes, just loop through, but make sure both $nn and $ss has same count, which is depending on your file.
Also Note: mysql_* functions has been deprecated, so please use mysqli or PDO instead
$count = count($nn);
for($i=0; $i < $count; $i++){
$sql = "INSERT INTO users(name, line) VALUES('$nn[$i]', '$ss[$i]')"; mysql_query($sql);
}
EDIT 2
try this example:
$file = array(
0 => 'name,name1',
1 => 'willhaveishere1',
2 => 'name,name2',
3 => 'willhaveishere2',
4 => 'name,name3',
5 => 'willhaveishere3'
);
$count = count($file);
if($count > 0) {
foreach($file as $row) {
$pos = strpos($row, ',');
if($pos !== false){
$nn[] = substr($row, $pos + 1);
} else {
$ss[] = $row;
}
}
}
echo '<pre>';
$count = count($nn);
for($i=0; $i < $count; $i++){
$sql = "INSERT INTO users(name, line) VALUES('$nn[$i]', '$ss[$i]');";
echo $sql.PHP_EOL;
}
You can try this straightforward method:
if($fh = fopen("file.txt","r")){
$nameBefore = false;
//loop through every line of your file
while (!feof($fh)){
$line = fgets($fh);
//check if the name was detected in previous line
if ($nameBefore !== false)
{
//you have the set of name and line, do what you want
echo $nameBefore . ': ' . $line . '<br />';
$nameBefore = false;
}
else
{
//see if the line is made of two coma separated segments and the first one is 'name'
//Remember the name for the next line
$parts = explode(',', $line);
if (count($parts) == 2 && $parts[0] == 'name')
$nameBefore = $parts[1];
}
}
fclose($fh);
}
One option is to use strpos to find the first occurrence of the character in the line, and if found remove everything from the line before that position. This way you are left with only the part of the line you are interested in.
Code:
$character = ',';
$fileHandle = fopen('file.txt', 'r');
while (!feof($fileHandle)) {
// Retrieve the line from the file
$line = fgets($fileHandle);
// If the line contains the character
// Remove everything before the character
$charPos = strpos($line, $character);
if ($charPos !== false) {
$line = substr($line, $charPos + 1);
}
// Do something with the remainder of the line
echo $line . PHP_EOL;
}
fclose($fileHandle);
Output:
name1
willhaveishere1
name2
willhaveishere2
name3
willhaveishere3
If you wish to retrieve the following line, simply do another retrieve line call in your loop:
while (!feof($fileHandle)) {
// Retrieve two lines in one loop iteration
$lineOne = fgets($fileHandle);
$lineTwo = fgets($fileHandle);
}
Making sure to only apply the comma replace part on the first line. This can lead to problems though if your data is... inconsistent.
Hope this helps.
I am trying to select columns in my tab separated document.
The problem i am having is i get this error....
Notice: Undefined offset: 1 in E:\xampp\htdocs\qrcode\tab.php on line 11
Notice: Undefined offset: 2 in E:\xampp\htdocs\qrcode\tab.php on line 12
Notice: Undefined offset: 3 in E:\xampp\htdocs\qrcode\tab.php on line 13
Code:
$file = "t_rac.txt";// Your Temp Uploaded file
$handle = fopen($file, "r"); // Make all conditions to avoid errors
$read = file_get_contents($file); //read
$lines = explode("\n", $read);//get
$i= 0;//initialize
$o=1;
foreach($lines as $key => $value){
$cols[$i] = explode("\t", $value);
$list=($cols[$o++][1]);
$list.=($cols[$o++][6]);
$list.=($cols[$o++][7]);
$i++;
}
echo $list;
Updated code off of data this works for ya.
I skip the 1st line, and make sure the column is not out of bounds before adding item.
$lines = explode("\n", $read); //get
$i = 0;//initialize
$list = "";
foreach($lines as $value)
{
if($i != 0)
{
$cols[$i] = explode("\t", $value);
if(isset($cols[$i][1]))
$list.=($cols[$i][1]);
if(isset($cols[$i][6]))
$list.=($cols[$i][6]);
if(isset($cols[$i][7]))
$list.=($cols[$i][7]);
$list.= "<br />";
}
$i++;
}
echo $list;
Your array keys are totally wrong:
$list=($cols[$o++][1]);
^^^^
That changes $o every time you use it. So on your first iteration of the loop, you're doing:
$list=($cols[1][1]);
$list.=($cols[2][6]);
$list.=($cols[3][7]);
Note that $o is now 4... On your second iteration you're doing
$list=($cols[4][1]);
$list.=($cols[5][6]);
$list.=($cols[6][7]);
and $o is 7 at the end of the iteration, etc... If you have 100 rows to process, $o will end up being 301, and you do NOT have that many columns in your $cols array.
May I suggest using the fgetcsv PHP function to parse character delimited files. There is no need to use explode.
Edit - I just read your other comment and understand now what you mean. Updated.
Edit - So you need to get columns 1, 6 and 7 from the same row then. Also added check for first row.
function getMeTheString($file) {
$str = "";
if (($handle = fopen($file, "r")) === FALSE) return;
$line = 0;
while (($cols = fgetcsv($handle, 1000, "\t")) !== FALSE) {
if ($line > 0) { // Ignore 1st line
$str .= $cols[1];
$str .= $cols[6];
$str .= $cols[7];
}
$line++;
}
return $str;
}
echo getMeTheString("t_rac.txt");
Thank you so much everyone! Here is the working code! Works like a charm #Demodave Thank you!!
<?php
$file = "t_rac.txt";// Your Temp Uploaded file
$handle = fopen($file, "r"); // Make all conditions to avoid errors
$read = file_get_contents($file); //read
$lines = explode("\n", $read); //get
$i = 0;//initialize
$list = "";
foreach($lines as $value)
{
if($i != 0)
{
$cols[$i] = explode("\t", $value);
if(isset($cols[$i][2]))
$list.=($cols[$i][2]);
$list.= "--";
if(isset($cols[$i][3]))
$list.=($cols[$i][3]);
$list.= "--";
if(isset($cols[$i][4]))
$list.=($cols[$i][4]);
$list.= "--";
if(isset($cols[$i][6]))
$list.=($cols[$i][6]);
$list.= "--";
if(isset($cols[$i][7]))
$list.=($cols[$i][7]);
$list.= "--";
if(isset($cols[$i][23]))
$list.=($cols[$i][23]);
$list.= "<br />";
}
$i++;
}
echo $list;
?>
i have the following code
$contents = file_get_contents('folder/itemtitle.txt');
$fnamedata = file_get_contents('folder/fname.txt');
$fnamearray = explode("\n", $fnamedata);
$contents = explode("\n", $contents);
foreach ($contents as $key => $itemline)
{
}
foreach ($fnamearray as $key2 => $fname)
{
echo ($fname);
echo ($itemline);
}
what i want to do is to have the first line of each file echo so the output looks like
fname[0},itemline[0],fname[1],itemline[1]
what i am getting with the following is just this
fname[0],fname[1],fname[2].... ect
h
Assuming the indexes will always match:
$contents = file_get_contents('folder/itemtitle.txt');
$fnamedata = file_get_contents('/home/b1396hos/public_html/ofwgkta.co.uk/dd_folder/fname.txt');
$fnamearray = explode("\n", $fnamedata);
$contents = explode("\n", $contents);
for($i = 0; $i < count($contents); $i++)
{
echo $fnamearray[$i];
echo $contents[$i];
}
Since both arrays are simple, consecutive numeric indexed arrays, you can just use a for loop:
$l = max(count($fnamedata),count($contents));
for($i=0; $i<$l; $i++) {
$itemline = $contents[$i];
$fname = $fnamearray[$i];
// do stuff
}