GoogleConverter

August 30, 2008

A fun little project that could work well in an “intro to practical programming” course:

filipp@probook.local [~]  > gc 14 in in cm
14 in = 35.56 centimeters

Wait, but we already have units(1)! Yes, but can it do this:

filipp@probook.local [~]  > gc 14,9 usd in eur
14,9 U.S. dollars = 10,1650976 Euros

All a whopping 8 lines of PHP (the working man’s language):

#!/usr/bin/env php
<?php

  array_shift ($argv);
  if (count ($argv) == 0) die ("Not enough arguments\n");
  $q = implode ("+", $argv);
  $c = file_get_contents ("http://www.google.com/search?q=$q");
  preg_match ('/<div id=res.+?<\/h2>/', $c, $r);
  $a = str_replace (' ', '', strip_tags ($r[0]));
  if ($a == "Search Results") $a = "No results";
  printf ("%s\n", $a);

?>

:)