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"