function geonames_populations ($countries
{
$data = file_get_contents"http://www.geonames.org/countryInfo?");
if ($data==false) {
throw new Exception"Data was not returned by geonames.org");
} else {
// parse the XML
$obj = simplexml_load_string$data);
// put countries in lower case
for ($ii=0;$iicount$countries); $ii++) {
$countries$ii]=strtolower$countries$ii]);
}
// get the population data for the matching countries
$all_countries = $obj->country;
$valuesarray();
foreach ($all_countries as $country) {
if (array_searchstrtolower$country->countryName), $countries)!==false) {
$country_name = $country->countryName.'';
$country_pop = $country->population+0;
$values$country_name] = $country_pop
}
}
return $values
}
}
Category: Algorithm
eXorithm – Execute Algorithm: View / Run Algorithm validate_url
function validate_url ($url
{
$regex = '/^(https?|ftp)://'; //protocol
$regex .= '(([a-z0-9$_.+!*'(),;?&=-]|%[0-9a-f]{2})+'; //username
$regex .= '(:([a-z0-9$_.+!*'(),;?&=-]|%[0-9a-f]{2})+)?'; //password
$regex .= '@)?'; //auth requires @
$regex .= '((([a-z0-9][a-z0-9-]*[a-z0-9].)*'; //domain segments AND
$regex .= '[a-z][a-z0-9-]*[a-z0-9]'; //top level domain OR
$regex .= '|((d|[1-9]d|1d{2}|2[0-4][0-9]|25[0-5]).){3}'
$regex .= '(d|[1-9]d|1d{2}|2[0-4][0-9]|25[0-5])'; //IP address
$regex .= ')(:d+)?'; //port
$regex .= ')(((/+([a-z0-9$_.+!*'(),;:@&=-]|%[0-9a-f]{2})*)*'; //path
$regex .= '(?([a-z0-9$_.+!*'(),;:@&=-]|%[0-9a-f]{2})*)'; //query string
$regex .= '?)?)?'; //path and query string optional
$regex .= '(#([a-z0-9$_.+!*'(),;:@&=-]|%[0-9a-f]{2})*)?'; //fragment
$regex .= '$/i'
return (preg_match$regex, $url) ? true : false);
}
eXorithm – Execute Algorithm: History For Algorithm stock_ticker
eXorithm – Execute Algorithm: View / Run Algorithm merge_sort
function merge_sort ($array
{
  if (count$array) <= 1) Â
    return $array; Â
 Â
  // sort each half
  $mid = floorcount$array)/2);
  $left = merge_sortarray_slice$array, 0, $mid)); Â
  $right = merge_sortarray_slice$array, $mid)); Â
 Â
  // merge the arrays
  $array = array();
  while (count$left)>0 && count$right)>0) { Â
    if ($left[0] <= $right[0]) { Â
      array_push$array, array_shift$left)); Â
    } else { Â
      array_push$array, array_shift$right)); Â
    } Â
  } Â
  $array = array_merge$array, $left, $right); Â
 Â
  return $array
}Â
eXorithm – Execute Algorithm: View / Run Algorithm highlight
function highlight ($text, $phrase, $highlighter
{
  if (empty$phrase)) {
    return $text
  }
 Â
  if (is_array$phrase)) {
    $replace = array();
    $with = array();
   Â
    foreach ($phrase as $key => $value) {
      $key = $value
      $value = $highlighter
      $key = '([s])(' . $key . ')([s.,!?<])'
      $replace[] = '|' . $key . '|ix'
      $with[] = empty$value) ? $highlighter : $value
    }
    return preg_replace$replace, $with, $text);
  } else {
    $phrase = '([s])(' . $phrase . ')([s])'
   Â
    return preg_replace'|'$phrase'|i', $highlighter, $text);
  }
}Â
eXorithm – Execute Algorithm: View / Run Algorithm lathe_shape
function lathe_shape ($equation, $start_x, $end_x, $detail, $degree_x, $degree_y, $degree_z, $face_color, $vertex_color, $scale, $image_size
{
$vdist = 20;
$dist = 20;
$sides = lathe_polygons$equation, $start_x, $end_x, $detail);
// project each of the polygons
for ($i=0; $icount$sides); $i++) {
$points[] = project_polygon$sides$i], $degree_x, $degree_y, $degree_z, ($end_x$start_x)/2, 0, 0, 20, 20, true);
}
// scale the image somewhat
$scale = $image_size$scale/($end_x$start_x);
return render_polygons$points, $vertex_color, $face_color, false, false, $image_size, $scale);
}
eXorithm – Execute Algorithm: View / Run Algorithm kmp_search
function kmp_search ($x, $y
{
// see http://www-igm.univ-mlv.fr/~lecroq/string/node8.html
// set-up phase
$i = 0;
$j = -1;
$kmpNext = array();
while ($i < $m) {
while ($j > -1 && $x[i] != $x[j])
$j = $kmpNext$j];
$i++;
$j++;
if ($x$i] == $x$j])
$kmpNext$i] = $kmpNext$j];
else
$kmpNext$i] = $j
}
// search phase
$i = 0;
$j = 0;
$m = strlen$x);
$n = strlen$y);
$results = array();
while ($j < $n) {
while ($i > -1 && $x$i] != $y$j])
$i = $kmpNext$i];
$i++;
$j++;
if ($i >= $m) {
$results[] = $j$i+1;
$i = $kmpNext$i];
}
}
return $results
}
eXorithm – Execute Algorithm: Algorithms Beginning with A
eXorithm – Execute Algorithm: View / Run Algorithm sorted
function sorted ($array
{
$len = count$array);
for$i=1; $i$len; $i++) {
if$array$i-1] > $array$i])
return false;
}
return true;
}
eXorithm – Execute Algorithm: View / Run Algorithm magic8ball
function magic8ball ()
{
$answers =array'It is certain', 'It is decidedly so', 'Without a doubt'
'Yes – definitely', 'You may rely on it', 'As I see it, yes'
'Most likely', 'Outlook good', 'Signs point to yes', 'Yes'
'Reply hazy, try again', 'Ask again later'
'Better not tell you now', 'Cannot predict now'
'Concentrate and ask again', 'Don't bet on it'
'My reply is no', 'My sources say no', 'Outlook not so good'
'Very doubtful' );
$index = rand(0, count$answers));
return ($answers$index]);
}