Show sourcecode
The following files exists in this folder. Click to view.
db_connect.php
dict.xml
practice.php
user.php
util.php
util.php
141 lines UTF-8 Windows (CRLF)
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?php
function encase($content, $tag, $class = "", $style = "")
{
return "<" . $tag .
" class='" . $class . "'" .
" style='" . $style . "'" . ">"
. $content .
"</" . $tag . ">";
}
function normalize_spanish_string($str)
{
$str = strtolower($str);
for ($i = 0; $i < strlen($str); $i++) {
switch ($str[$i]) {
case "á":
$str[$i] = "a";
break;
case "é":
$str[$i] = "e";
break;
case "í":
$str[$i] = "i";
break;
case "ñ":
$str[$i] = "n";
break;
case "ó":
$str[$i] = "o";
break;
case "ü":
$str[$i] = "u";
break;
case "ú":
$str[$i] = "u";
break;
}
}
return $str;
}
function path_to_url($path)
{
$path = str_replace("//", "/", str_replace($_SERVER['DOCUMENT_ROOT'], '/', str_replace("\\", "/", $path)));
return $path;
}
function produce_XML_object_tree($raw_XML)
{
libxml_use_internal_errors(true);
try {
$xmlTree = new SimpleXMLElement($raw_XML);
} catch (Exception $e) {
// Something went wrong.
$error_message = 'SimpleXMLElement threw an exception.';
foreach (libxml_get_errors() as $error_line) {
$error_message .= "\t" . $error_line->message;
}
trigger_error($error_message);
return false;
}
return $xmlTree;
}
function sort_cmp($a, $b)
{
setlocale(LC_COLLATE, 'es_ES.UTF-8');
return strcoll($a["es"], $b["es"]);
}
function get_predefined_translation(string $word)
{
static $EN_ES_translations = [];
if ($EN_ES_translations == []) {
$xml_feed_url = path_to_url("http://localhost/" . BASE_PATH . "/util/dict.xml");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $xml_feed_url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$xml = curl_exec($ch);
curl_close($ch);
$cont = produce_XML_object_tree($xml);
foreach ($cont->l as $letter) {
foreach ($letter->w as $wordXml) {
$EN_ES_translations[] = [
"es" => strtolower($wordXml->c),
"en" => strtolower($wordXml->d),
];
}
}
usort($EN_ES_translations, "sort_cmp");
}
// binary search
$pointer = ceil(count($EN_ES_translations) / 2);
$i = 2;
$found = true;
while ($EN_ES_translations[$pointer]) {
if ($i > 100) {
$found = false;
break;
}
//echo $pointer."<br>";
$cmp = strcasecmp($EN_ES_translations[$pointer]["es"], $word);
if ($cmp == 0)
break;
if ($cmp > 0)
$pointer = $pointer - ceil(count($EN_ES_translations) / pow(2, $i));
else
$pointer = $pointer + ceil(count($EN_ES_translations) / pow(2, $i));
$i++;
}
// not found in array
if (!$found)
return null;
// look for multiple definitions
$pointers = [$pointer];
$pointerR = $pointer + 1;
$pointerL = $pointer - 1;
while ($pointerR < count($EN_ES_translations) && $EN_ES_translations[$pointerR]["es"] == $EN_ES_translations[$pointer]["es"])
$pointers[] = $pointerR++;
while ($pointerL >= 0 && $EN_ES_translations[$pointerL]["es"] == $EN_ES_translations[$pointer]["es"])
$pointers[] = $pointerL--;
$translations = [];
foreach ($pointers as $pointer) {
$translations[] = $EN_ES_translations[$pointer]["en"];
}
return $translations;
}