tags:

views:

63

answers:

4

So I have a huge file of names that has to be split into more columns for excel.

So I am using PHP to parse this, so here is part of the example:

Dr. Johnny Apple Seed
Ms. A. P. Oranges
Mrs. Purple Cup

Essentially, I want to put this into an array. I was using fopen, fget, and explode. The problem is, if I use explode, some of the arrays would not have consistent elements. For example, the first one would have a total of 4 (Dr, Johnny, Apple, Seed), the third would have only three. How would I add a fourth empty element between purple and cup? or is there a better way?

This is my code:

$fp = fopen($filename,"r");

if ($fp) { 
   while (!feof($fp)) { 
  $data = fgets($fp, filesize($filename));
  $contents[] = explode(" ", $data) . 
   } 

} 

fclose($fp);
echo "<pre>";
var_dump($contents);
echo "</pre>";

Desired Output:

  array(4) {
    [0]=>
    string(4) "Mrs."
    [1]=>
    string(4) "Purple"
    [2]=>
    string(1) " "
    [3]=>
    string(8) "Cup"

array(4) {
    [0]=>
    string(3) "Dr."
    [1]=>
    string(6) "Johnny"
    [2]=>
    string(5) "Apple"
    [3]=>
    string(4) "Seed"
A: 

If they are newlines, you can just use "\n" as the $needle

David Weitz
+1  A: 

Try this (haven't tested but should work):

// Define the clean array
$clean_array = array();

$fp = fopen($filename,"r");
if ($fp) { 
    while (!feof($fp)) { 
        $data = fgets($fp, filesize($filename));
        // use trim() on $data to avoid empty elements
        $contents = explode(" ", trim($data));

        if (count($contents) == '3') {
            // Copy last element to new last element
            $contents[] = $contents[2];
            // Clear the old last element
            $contents[2] = ' ';
        }
        $clean_array[] = $contents;
    } 
} 
fclose($fp);

echo '<pre>';
print_r($clean_array);
echo '</pre>';

Hope this helps.

FreekOne
+1  A: 

This should work:

<?php
$new_array = array();
$fp = fopen($filename, "r");
if ($fp) {
    while (!feof($fp)) {
        $data = fgets($fp, filesize($filename));
        $contents[] = explode(" ", $data);
    }
}
foreach ($contents as $content) {
    if (count($content) != 4) {
        $content[3] = $content[2];
        $content[2] = " ";
        $new_array[] = $content;
    }
    else {
        $new_array[] = $content;
    }
}
fclose($fp);
echo "<pre>";
var_dump($new_array);
echo "</pre>";
?>
Wolfy
What's the point of looping twice over what's basically the same data ?
FreekOne
My mistake, it can be done in one loop...
Wolfy
A: 

I used array_splice, here is my solution:

<?php

$filename = "excel-names.txt";
$fp = fopen($filename,"r");

if ($fp) { 
   while (!feof($fp)) { 
        $data = fgets($fp, filesize($filename));
        $contents[] = explode(" ", $data, 4);
    }    
} 

fclose($fp);

foreach( $contents as $a) {
    $arrayCount = count($a);

    if( $arrayCount == 3 ) {
        array_splice($a, 2, 0, " ");
    }
    if( $arrayCount == 2 ) {
        array_splice($a, 0, 0, " ");
        array_splice($a, 2, 0, " ");
    }
}

?>
Doug
You are also iterating twice over the data set -- once when you read the file and once more over the `$contents` array. While this may get the job done, I wouldn't really recommend it, especially when working with very large files like it was your case. Either way, glad you solved it :)
FreekOne
Wisdom acknowledged. How would you change my source? Could you show me?
Doug