On Thu, 29 Jun 2000, Drew Taylor wrote:

> I'm hoping it's been done already, because the user-agent strings are
> terribly inconsistent...

I needed something like that once and ended up with this:

sub UA { 
  my $ua = shift;
  my $n = "";
  my $v = "";

  # $n is the name of the browser
  # $v the version
  $ $p the platform
  # $ua the full useragent string

  # grap the beginning of the UA string for the name
  ($n) = ($ua =~ m/^([^\/]+)\//);

  # but everything calls itself Mozilla, so let's check for a few more
  # known browsers
  $n = "msie" if m/MSIE/;
  # it's worse, these babies calls themself both MSIE and Mozilla, doh!
  $n = "webtv" if m/webtv/i;
  $n = "opera" if m/opera/i;

  # try getting the version
  ($v) = ($ua =~ m/\/((\d+)(\.\d+)?)/)[0];
  # loosen up what we match on a little and try again if we didn't get
  # a match
  ($v) = ($ua =~ m/((\d+)(\.\d+)?)/)[0] unless $v;

  # these thingies call themself Mozilla/N.Z, so let's get their real
  # version number
  ($v) = (m/MSIE ((\d+)(\.\d+)?)/) if ($n eq "msie");
  ($v) = (m/webtv\/((\d+)(\.\d+)?)/i) if ($n eq "webtv");
  ($v) = (m/opera[\s\/]((\d+)(\.\d+)?)/i) if ($n eq "opera");

  # guess the platform
  my $p = "";
  $p = "w" if $ua =~ m/win/i;
  $p = "w16" if $ua =~ m/win(dows)?\s?3\.1/i;
  $p = "w16" if $ua =~ m/win.*16(bit)?/i;
  $p = "w95" if $ua =~ m/win(dows)?\s?95/i;
  $p = "w98" if $ua =~ m/win(dows)?\s?98/i;
  $p = "wnt" if $ua =~ m/win(dows)?\s?NT/i;
  $p = "w32" if $ua =~ m/win(dows)?\s?32/i;
  $p = "x11" if $ua =~ m/X11/;
  $p = "mac" if $ua =~ m/mac/i;
  $p = "os2" if $ua =~ m/os\/2/i;
  $p = "webtv" if $n eq "webtv";

  $n = lc($n);
  die "No name for $ua?\n" unless $n;
  #return $n, $v, $p;
  return "$n-$v-$p";
}


-- 
ask bjoern hansen - <http://www.netcetera.dk/~ask/>
more than 70M impressions per day, <http://valueclick.com>


Reply via email to