-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontact-parser.php
More file actions
84 lines (76 loc) · 2.34 KB
/
contact-parser.php
File metadata and controls
84 lines (76 loc) · 2.34 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
function getContacts($filename) {
$handle = fopen($filename, 'r');
$contactList = [];
clearstatcache();
$contents = fread($handle, filesize($filename));
$contents = str_replace("|", "\n", $contents);
$contacts = explode("\n", $contents);
foreach ($contacts as $data) {
if (!is_numeric($data)) {
$personalInfo = array();
$personalInfo["name"] = $data;
} else {
$data = substr($data, 0, 3) . "-" . substr($data, 3, 3) . "-" . substr($data, 6, 4);
$personalInfo["number"] = $data;
array_push($contactList, $personalInfo);
}
}
fclose($handle);
return $contactList;
}
function addNew($filename) {
echo 'Enter a name'.PHP_EOL;
$newName = fgets(STDIN);
echo 'Enter a number'.PHP_EOL;
$newNum = substr((fgets(STDIN)), 0, 10);
$handle = fopen($filename, 'a');
$newContact = trim($newName) . "|" . trim($newNum).PHP_EOL;
$contents = fwrite($handle, $newContact);
fclose($handle);
}
function search($filename) {
$findMe = fgets(STDIN);
$handle = fopen($filename, 'r');
$contents = fread($handle, filesize($filename));
$found = substr($contents, strpos($contents, trim($findMe)), strlen($findMe) + 11);
fclose($handle);
return $found;
}
function delete($filename) {
echo 'Enter a name to remove: ';
$findMe = fgets(STDIN);
$handle = fopen($filename, 'r');
$contents = fread($handle, filesize($filename));
fclose($handle);
$handle = fopen($filename, 'w');
$deleteMe = substr($contents, strpos($contents, trim($findMe)), strlen($findMe) + 11);
$contents = str_replace($deleteMe, "", $contents);
$contents = fwrite($handle, $contents);
fclose($handle);
}
do {
echo "
1. View contacts.
2. Add a new contact.
3. Search a contact by name.
4. Delete a name.
5. Exit.
Enter an option (1, 2, 3, 4, 5):".PHP_EOL;
$option = fgets(STDIN);
switch ($option) {
case 1:
print_r(getContacts('contacts.txt'));
break;
case 2:
addNew('contacts.txt');
break;
case 3:
echo 'Enter a name to Search: ';
echo "***" . search('contacts.txt') . "***";
break;
case 4:
delete('contacts.txt');
break;
}
} while ($option != 5);