Strawberry
Strawberry

Reputation: 67968

Suggestions for parsing this data

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"

Upvotes: 2

Views: 112

Answers (4)

Strawberry
Strawberry

Reputation: 67968

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, " ");
    }
}

?>

Upvotes: 0

Wolfy
Wolfy

Reputation: 4383

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>";
?>

Upvotes: 1

Valentin Flachsel
Valentin Flachsel

Reputation: 10825

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.

Upvotes: 1

David Weitz
David Weitz

Reputation: 461

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

Upvotes: 0

Related Questions