-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact-parser.php
More file actions
32 lines (26 loc) · 933 Bytes
/
contact-parser.php
File metadata and controls
32 lines (26 loc) · 933 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
function parseContacts($filename)
{
$handle = fopen($filename, 'r');
// todo - read file and parse contacts
$contacts = fread($handle, filesize($filename));
// now convert into an array
$array = explode("\n", $contacts);
// add new line at the end of each string on array
array_pop($array);
$filename = 'contacts.txt';
$newContacts = [];
fclose($handle);
// variable = $array and $name = $key in foreach loop
foreach ($array as $name) {
$newArray = [];
// new var for the pipe breaking point of key
$persInfo = explode("|", $name);
$persName = $persInfo[0];
$persNumber = substr($persInfo[1], 0, 3) . "-" . substr($persInfo[1], 3, 3) . "-" . substr($persInfo[1], 6, 4);
$newArray = ["name" => $persName, "number" => $persNumber];
array_push($newContacts, $newArray);
}
return $newContacts;
}
var_dump(parseContacts('contacts.txt'));