blur_image    version 0.1    Blur an image.
Category: Algorithm
Weather Forecast
<?php /** * weather_forecast * * Display the weather for a location. Look up using the Google weather API. * * @version 1.4 * @author Contributors at eXorithm * @link /algorithm/view/weather_forecast Listing at eXorithm * @link /algorithm/history/weather_forecast History at eXorithm * @license /home/show/license * * @param mixed $location The location to get the weather for. Can be a city, zip code, or postal code. * @param mixed $units The units to get the temperature in. * @param string $header_color (hex color code) Color of the header * @param string $day_header_color (hex color code) Color of the header for each of the days * @param string $day_color (hex color code) Color of the cells containing the forecasts * @return string HTML */ function weather_forecast($location='Sydney, Australia',$units='c',$header_color='f0f0f0',$day_header_color='d0ffd0',$day_color='f0f0f0') { $weather = file_get_contents('http://www.google.com/ig/api?weather='.urlencode($location)); $xml = simplexml_load_string($weather); if (!isset($xml->weather->forecast_conditions)) { throw new Exception('Data could not be retreived for location '.$location); } $location = $xml->weather->forecast_information->city['data']; // four day outlook for ($i = 0; $i < 4; $i++){ if ($xml->weather->forecast_conditions->$i) { $forecast_day[] = $xml->weather->forecast_conditions->$i->day_of_week['data']; $forecast_condition[] = $xml->weather->forecast_conditions->$i->condition['data']; $low = $xml->weather->forecast_conditions->$i->low['data']; if ($units=='k') $low = round((($low - 32)*5/9) + 273, 1); else if ($units=='c') $low = round(($low - 32)*5/9, 1); $forecast_low[] = $low; $high = $xml->weather->forecast_conditions->$i->high['data']; if ($units=='k') $high = round((($high - 32)*5/9) + 273, 1); else if ($units=='c') $high = round(($high - 32)*5/9, 1); $forecast_high[] = $high; $forecast_icon[] = $xml->weather->forecast_conditions->$i->icon['data']; } } // current $condition = $xml->weather->current_conditions->condition['data']; $temp = $xml->weather->current_conditions->temp_f['data']; if ($units=='k') $temp = round((($temp - 32)*5/9) + 273, 1); else if ($units=='c') $temp = round(($temp - 32)*5/9, 1); $icon = $xml->weather->current_conditions->icon['data']; // build the HTML $header = "<tr><td colspan=\"".count($forecast_day)."\" bgcolor=\"#$header_color\">"; if ($icon!='') $header .= "<img align=\"left\" src=\"http://www.google.com$icon\">"; $header .= "<b>$location</b><br>Currently <i>$condition</i> <b>$temp$units</b>"; $header .= "</td></tr>\n<tr>"; $data = "<tr>\n"; for ($i = 0; $i < count($forecast_day); $i++){ $header .= "<td width=\"130\" bgcolor=\"#$day_header_color\"><b>$forecast_day[$i]</b></td>"; $data .= "<td bgcolor=\"#$day_color\">"; $data .= "<img src=\"http://www.google.com$forecast_icon[$i]\">"; $data .= "<br><i>$forecast_condition[$i]</i>"; $data .= "<br>high <b>$forecast_high[$i]$units</b>"; $data .= "<br>low <b>$forecast_low[$i]$units</b>"; $data .= "</td>\n"; } $header .= "</tr>"; $data .= "</tr>"; return "<table cellpadding=\"5\" cellspacing=\"3\">\n$header\n$data\n</table>"; } ?>
eXorithm – Execute Algorithm: View / Run Algorithm ascii_art
function ascii_art ($img
{
$width = imagesx$img);
$height = imagesy$img);
$ascii = '<pre style="font-size:8px;line-height:1px;letter-spacing:-2px;">'
for$y=0;$y$height$y++)
{
for$x=0;$x$width$x++)
{
$c = imagecolorat$img, $x, $y);
$r = ($c >> 16) & 0xFF;
$g = ($c >> 8) & 0xFF;
$b = $c & 0xFF;
$t = ($c >> 24) & 0xFF;
if ($t>=0)
{
/** transparency **/
$r = $r+(255-$r)*$t/127;
$g = $g+(255-$g)*$t/127;
$b = $b+(255-$b)*$t/127;
}
$r = floor$r/16);
$g = floor$g/16);
$b = floor$b/16);
$ascii .= '<span style="color:#'dechex$r).dechex$g).dechex$b).'">#</span>'
}
$ascii .= '<br>'
}
$ascii .= '</pre>'
return $ascii
}
eXorithm – Execute Algorithm: View / Run Algorithm project_polygon
function project_polygon ($points, $degree_x, $degree_y, $degree_z, $center_x, $center_y, $center_z, $dist1, $dist2, $include_z
{
  // check points
  if ((count$points)%3)!=0) {
    throw new Exception'The points must be a list like x1, y1, z1, x2, y2, z2, etc. The number of points therefore must be divisible by three.');
  }
 Â
  $degree_x = deg2rad$degree_x);
  $degree_y = deg2rad$degree_y);
  $degree_z = deg2rad$degree_z);
 Â
  $cosx = cos$degree_x);
  $sinx = sin$degree_x);
  $cosy = cos$degree_y);
  $siny = sin$degree_y);
  $cosz = cos$degree_z);
  $sinz = sin$degree_z);
 Â
  $array = array();
 Â
  for ($i=0;$icount$points);$i$i+3) {
    $x0 = $points$i]-$center_x
    $y0 = $points$i+1]-$center_y
    $z0 = $points$i+2]-$center_z
   Â
    $x1 = $cosy*($sinz$y0 + $cosz$x0) - $siny$z0
    $y1 = $sinx*($cosy$z0 + $siny*($sinz$y0 + $cosz$x0)) + $cosx*($cosz$y0 - $sinz$x0);
    $z1 = $cosx*($cosy$z0 + $siny*($sinz$y0 + $cosz$x0)) - $sinx*($cosz$y0 - $sinz$x0);
 Â
    $x2 = $x1$dist1/($z1$dist1$dist2);
    $y2 = $y1$dist1/($z1$dist1$dist2);
    $z2 = $z1$dist1/($z1$dist1$dist2);
 Â
    $array[] = $x2
    $array[] = $y2
    if ($include_z) $array[] = $z2
  }
 Â
  return $array
}Â
eXorithm – Execute Algorithm: History For Algorithm weather_forecast
weather_forecast    version 1.4    Display the weather for a location. Look up using the Google weather API.
eXorithm – Execute Algorithm: View / Run Algorithm word_counts
function word_counts ($text, $noise
{
  $words = preg_split'/[^A-Za-z]+/', strtolower$text));
  $counts = array();
 Â
  foreach ($words as $word) {
    if (strlen$word)>1) { // 1-letter words are ignored
      if (array_search$word, $noise)===false) { // noise word?
        if (array_key_exists$word, $counts)) {
          $counts$word] = $counts$word]+1;
        } else {
          $counts$word] = 1;
        }
      }
    }
  }
 Â
  return $counts
}Â
eXorithm – Execute Algorithm: View / Run Algorithm sum_list
function sum_list ($numbers
{
return array_sum$numbers);
}
eXorithm – Execute Algorithm: View / Run Algorithm validate_email
function validate_email ($email
{
if (preg_match"%^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$%i", $email))
return true;
else
return false;
}
eXorithm – Execute Algorithm: View / Run Algorithm binary_search
function binary_search ($array, $item, $low, $high
{
if ($low==-1) $low = 0;
if ($high==-1) $high = count$array)-1;
if ($low > $high) {
// item not found
return -1;
}
// get the middle
$middle = floor(($low$high)/2);
if ( $array$middle] == $item ) {
// found it
return $middle
} elseif ($item < $array$middle]) {
// search left
return binary_search$array, $item, $low, $middle-1);
} else {
// search right
return binary_search$array, $item, $middle+1, $high);
}
}