The script is intended to return an array with texts containing specific words in English and the equivalent texts in Polish from EUR-Lex - a website with EU documents.
The script downloads the page which address is specified by the passed variable. Then this page is parsed using regular expressions for the document codes. This code are then used to access another page. This page is then parsed for elements contained within <tr>
tags, which, if their content contains words from the variable (checked with a regular expression), are saved in an array.
For variables containing 1-2 words the script works fairly well; not so well for more. I got complaints from my free hosting provider (hostinger.in) that the script uses too much resources. I have other scripts run before that script, but not simultaneously, so I don't if they contribute to the problem.
<?php
//test variables
//$var1 = 'board_of_directors';
//$var1 = 'affidavit';
$var1 = 'event_of_default';
$var1 = explode("_", $var1);
$words = implode(" ",$var1);
if (count($var1) >= 2) {
$var1 = implode("%20",$var1);
} else {
$var1 = implode("%",$var1);
}
$var1 = "\""."$var1"."\"";
$html = file_get_contents("http://eur-lex.europa.eu/search.html?qid=1&text=$var1&scope=EURLEX&type=quick&lang=en");
preg_match_all('/CELEX:(\w+)&/si', $html, $matches2);
$result = array_unique($matches2[1]);
$result2 = file_get_contents("http://eur-lex.europa.eu/legal-content/EN-PL/TXT/?uri=CELEX:$result[0]&from=EN");
$dom = new domDocument;
libxml_use_internal_errors(true);
$dom->loadHTML($result2);
$items = $dom->getElementsByTagName('tr');
$return_array = array();
for ($i = 0; $i < $items->length - 1; $i++) {
$text1 = $items->item($i)->childNodes->item(0)->textContent;
if (preg_match('/' . $words . '/i', $text1)) {
$text2 = $items->item($i)->childNodes->item(1)->textContent;
array_push($return_array, $text1);
array_push($return_array, $text2);
}
}
echo json_encode($return_array);
?>
preg_match
on<tr>
s of the 2nd one, rather than the uniquepreg_match_all
on the 1st one. That said, I feel puzzled: since this 2nd HTML page is pretty heavy, anyway it might take time to analyze it whatever the number of involved words. So I"m surprised that it works fairly well for only 1 or 2 words. \$\endgroup\$