forked from paulhibbert/anagram
-
Notifications
You must be signed in to change notification settings - Fork 0
/
loadwords.php
73 lines (64 loc) · 2.22 KB
/
loadwords.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<!doctype html>
<html lang="en">
<?php
$filenames = array("2cities.txt", "3men.txt", "alice.txt", "crusoe.txt", "darkness.txt", "dracula.txt", "earnest.txt", "expectations.txt", "frankenstein.txt", "holmes.txt", "liberty.txt", "machine.txt", "middlemarch.txt", "peterrabbit.txt", "pride.txt", "screw.txt", "styles.txt", "treasure.txt", "tsawyer.txt", "worlds.txt");
$max = 10;
$int_options = array("options"=> array("min_range"=>6, "max_range"=>$max));
$found = 0;
$unique_words = array();
foreach($filenames as $filename){
get_words($filename);
}
$output = print_r($unique_words, true);
file_put_contents('words.txt', $output);
echo("Found: " . $found . " unique words.");
include_once 'Database.php';
include_once 'Word.php';
// Instantiate DB & connect
$database = new Database();
$db = $database->connect();
foreach($unique_words as $unique_word){
// Instantiate blog post object
$word = new Word($db);
$word->word = $unique_word;
// Create word entry
if($word->create()) {
echo json_encode(
array('message' => 'Word Created')
);
} else {
echo json_encode(
array('message' => 'Word Not Created')
);
}
}
function get_words($fname) {
$myfile = fopen($fname, "r") or die("Unable to open file!");
$buffer = fread($myfile,filesize($fname));
fclose($myfile);
if (strlen($buffer)) // if there is anything in the buffer
{
$words = str_word_count($buffer,1);
global $unique_words, $int_options, $found;
for($x=0;$x<count($words);$x++)
{
if (filter_var(strlen($words[$x]), FILTER_VALIDATE_INT, $int_options)) // if word length between min and max
{
$str = strtolower($words[$x]); // convert to lower case
if (preg_match("/^[a-z]+$/",$str)) // only a to z ok
{
if (!in_array($str,$unique_words))
{
$unique_words[] = $str;
$found++;
}
}
}
}
}
else
{
die("no buffer");
}
}
?>