<?php
// rangegeo.php
// countries.geojson
//  data-iso2="XX"
//  <area 
//   are 
//   ]
//   [
//   '></area>
  
  $minlat=0;
  $minlong=0;
  $maxlat=0;
  $maxlong=0;
  $cstuff='';
  $delim='';
  $valafters='';
  
  $bigxy=file_get_contents('./countries.html');
  $smallxy=file_get_contents('./image_chart.php');
  $recs=explode(' data-iso2="' . $_GET['ccode'] . '"', $bigxy);
  $records=explode(',' . $_GET['ccode'] . ',', $smallxy);
  if (sizeof($recs) > 1 && sizeof($records) > 1) {
    $valafters=explode(' value=', $records[0])[-1 + sizeof(explode(' value=', $records[0]))];
    for ($ii=1; $ii<sizeof($recs); $ii++) {
      $cstuff.=$delim . str_replace('[','',str_replace(']','',explode("'", explode("ates are", $recs[$ii])[1])[0]));
      $delim=',';
    }
    $ccs=explode(',', $cstuff);
    if (sizeof($ccs) > 1) {
  $minlat=$ccs[1];
  $minlong=$ccs[0];
  $maxlat=$ccs[1];
  $maxlong=$ccs[0];
      for ($ij=2; $ij<sizeof($ccs); $ij+=2) {
        if ($minlat > $ccs[1 + $ij]) {  $minlat=$ccs[1 + $ij];  }
        if ($maxlat < $ccs[1 + $ij]) {  $maxlat=$ccs[1 + $ij];  }
        if ($minlong > $ccs[0 + $ij]) {  $minlong=$ccs[0 + $ij];  }
        if ($maxlong < $ccs[0 + $ij]) {  $maxlong=$ccs[0 + $ij];  }
      }
      $from=$valafters . ',' . $_GET['ccode'] . ',';
      if (strpos($from, ' title=') === false && strpos($from, ' data-geo=') !== false) {
      $to=str_replace(' data-geo=', ' title=' . "\\\"" . str_replace(' ','',('' . $minlat . ',' . $minlong . ',' . $maxlat . ',' . $maxlong)) . "\\\" data-geo=", $from); // ... ' . $valafters;
      file_put_contents('./image_chart.php', str_replace($from, $to, $smallxy));
      echo $to;
      }
    }
  }       
?>
