-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadd_simple.php
43 lines (37 loc) · 1.02 KB
/
add_simple.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
<?php
if (count($argv) == 3) {
echo "Good\n";
}
else {
echo "You didn't enter the right number of arguments\n";
echo "You need to enter two words.\n";
die();
}
// Load the big ol' word list.
// todo: load in from a csv, first column is key
$words = [];
$handle = fopen("words.txt", "r") or die("Unable to open file for reading.\n");
if ($handle) {
while (($line = fgetcsv($handle)) !== false) {
// process the line read.
$key = array_shift($line);
$words[$key] = $line;
}
fclose($handle);
}
// Add the new word and save the file.
if (array_key_exists($argv[1], $words)) {
echo "Existing word!\n";
array_push( $words[$argv[1]] , $argv[2] );
}
else {
echo "New word!\n";
$words[$argv[1]] = [$argv[2]];
}
$words[$argv[1]] = array_unique($words[$argv[1]]);
$handle = fopen("words.txt", "w") or die("Unable to open file for writing.\n");
foreach($words as $primary => $secondary) {
array_unshift($secondary,$primary);
fputcsv($handle, $secondary);
}
fclose($handle);