Viewed   240 times

I'm trying parse a tab delemited text file into a set of PHP arrays, and help would be very much appreciated.

The .txt wil look like this (tab delimited not spaces)

data1a data1b data1c data1d
data2a data2b data2c data2d
data3a data3b data3c data3d
data4a data4b data4c data4d

and so on

I wish the PHP arrays to look like this

$arrayA = array('data1a', 'data2a', 'data3a', 'data4a');
$arrayB = array('data1b', 'data2b', 'data3b', 'data4b');
$arrayC = array('data1c', 'data2c', 'data3c', 'data4c');
$arrayD = array('data1d', 'data2d', 'data3d', 'data4d');

And I need the .txt file uploaded by a simple html form, e.g.

<form action="form.php" method="post" enctype="multipart/form-data">
  <label for="file">Filename:</label>
  <input type="file" name="file" id="file" /> 
  <input type="submit" name="submit" value="Submit" />
</form>

Any ideas on the code to place inside form.php?

Many thanks!

 Answers

5

Consider the content of your text.txt file

FristLineFirstData  FirstLineSecondData FirstLineThirdData
SecondLineFirstData SecondLineSecondData    SecondLineThirdData

Tab separed.

And the script :

<?php
$file = "text.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
foreach($lines as $key => $value){
    $cols[$i] = explode("t", $value);
    $i++;
}
echo "<pre>";
print_r($cols); //explore results
echo "</pre>";
?>

will return

Array
(
    [0] => Array
        (
            [0] => FristLineFirstData
            [1] => FirstLineSecondData
            [2] => FirstLineThirdData
        )

    [1] => Array
        (
            [0] => SecondLineFirstData
            [1] => SecondLineSecondData
            [2] => SecondLineThirdData
        )

)
Thursday, November 24, 2022
 
5

fputcsv() only writes one line at a time. Not the whole file. You you need to loop through $data in order to add all of that data into your CSV file.

$fp = fopen('file.csv', 'w');
foreach ($data as $line) {
    fputcsv($fp, $line);
}
fclose($fp);

A full example using your code:

$handle = fopen("fileurl.com", "r");
$lines = [];
if (($handle = fopen("fileurl.com", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, "t")) !== FALSE) {
        $lines[] = $data;
    }
    fclose($handle);
}
$fp = fopen('file.csv', 'w');
foreach ($lines as $line) {
    fputcsv($fp, $line);
}
fclose($fp);
Tuesday, November 22, 2022
 
merter
 
4

use this code, JS File (test.js)

for(var i=0;i<jArray.length;i++){
alert(jArray[i]);
}

PHP File (test.php)

<?php 
      $a = array("Apple","Orange","Grape");
?>
<script type="text/javascript">var jArray =<?php echo json_encode($a); ?>;</script>
<script type="text/javascript" src="test.js"></script>
Friday, August 26, 2022
 
2

They are already in arrays: $name is an array, as is $email

So all you need to do is add a bit of processing to attack both arrays:

$name = $_POST['name'];
$email = $_POST['account'];

foreach( $name as $key => $n ) {
  print "The name is ".$n." and email is ".$email[$key].", thank youn";
}

To handle more inputs, just extend the pattern:

$name = $_POST['name'];
$email = $_POST['account'];
$location = $_POST['location'];

foreach( $name as $key => $n ) {
  print "The name is ".$n.", email is ".$email[$key].
        ", and location is ".$location[$key].". Thank youn";
}
Tuesday, November 15, 2022
 
stevel
 
1

you can use my updated code and as per my demo it is working perfect for multiple file upload

 <?php
if(isset($_FILES['documents'])){

foreach($_FILES['documents']['tmp_name'] as $key => $tmp_name)
{
    $file_name = $key.$_FILES['documents']['name'][$key];
    $file_size =$_FILES['documents']['size'][$key];
    $file_tmp =$_FILES['documents']['tmp_name'][$key];
    $file_type=$_FILES['documents']['type'][$key];  
    move_uploaded_file($file_tmp,"galleries/".time().$file_name);
}
}else{
echo "<form enctype='multipart/form-data' action='test1.php' method='POST'>";
 echo "File:<input name='documents[]' multiple='multiple' type='file'/><input type='submit' value='Upload'/>";

 echo "</form>";
}
?>
Friday, December 2, 2022
 
lv10
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :