2
\$\begingroup\$

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);

?>
\$\endgroup\$
  • \$\begingroup\$ Reviewing the HTML pages you gave as example, I guess that the main issue should come from looping preg_matchon <tr>s of the 2nd one, rather than the unique preg_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\$ – cFreed Jul 16 '15 at 10:58
1
\$\begingroup\$

There's a few things you can do to improve here:

  • Use curl instead of get_file_contents, it speeds the document retrieval up so much.
  • The following could be converted to a ternary statement:
if (count($var1) >= 2) {
    $var1 = implode("%20",$var1);
} else {
    $var1 = implode("%",$var1);
}

Notice that the condition is reversed:

$var1 = implode('%' + (!count($var1) >= 2 ?: "20"), $var1);
  • You shouldn't be joining strings like this: "\""."$var1"."\"":

Do this instead: "\"{$var1}\"" (see that the variable is wrapped in curly braces)

  • Don't array_push, use the $array[] notation instead.

    $return_array[] = $text1;
    $return_array[] = $text2;
    
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.