-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact-parser.php
58 lines (34 loc) · 916 Bytes
/
contact-parser.php
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
<?php
function parseContacts($filename)
{
$contacts = [
0 => [
'name' => 'runs',
'number' => 'never'
]
];
// todo - read file and parse contacts
$handle = fopen($filename, 'r');
$contents = fread($handle, filesize($filename));
trim($contents);
fclose($handle);
// var_dump($contents);
$contents = explode("\n", $contents);
// var_dump($contents);
foreach ($contents as $key => $profile) {
if (empty($profile)) {
continue;
}
$tempArr = explode('|', $profile);
$contacts[$key]['name'] = $tempArr[0];
$contacts[$key]['number'] = phoneNumDash($tempArr[1]);
}
return $contacts;
}
// this function formates a phone number
function phoneNumDash($phonestrg) {
$phonestrg = chunk_split($phonestrg, 3, "-");
$phonestrg = substr_replace($phonestrg, substr($phonestrg,-2,1), -3);
return $phonestrg;
};
var_dump(parseContacts('contacts.txt'));