1   <?php
  2   /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3   // +----------------------------------------------------------------------+
  4   // | PHP Version 4                                                        |
  5   // +----------------------------------------------------------------------+
  6   // | Copyright (c) 1997-2004 The PHP Group                                |
  7   // +----------------------------------------------------------------------+
  8   // | This source file is subject to version 3.0 of the PHP license,       |
  9   // | that is bundled with this package in the file LICENSE, and is        |
  10   // | available at through the world-wide-web at                           |
  11   // | http://www.php.net/license/3_0.txt.                                  |
  12   // | If you did not receive a copy of the PHP license and are unable to   |
  13   // | obtain it through the world-wide-web, please send a note to          |
  14   // | license@php.net so we can mail you a copy immediately.               |
  15   // +----------------------------------------------------------------------+
  16   // | Authors: Philippe Jausions <Philippe.Jausions@11abacus.com>          |
  17   // |          Aidan Lister <aidan@php.net>                                |
  18   // +----------------------------------------------------------------------+
  19   //
  20   // $Id: version_compare.php,v 1.1 2004-10-26 18:22:16 kostja Exp $
  21   //
  22  
  23  
  24   /**
  25    * Replace version_compare()
  26    *
  27    * @category    PHP
  28    * @package     PHP_Compat
  29    * @link        http://php.net/version_compare
  30    * @author      Philippe Jausions <Philippe.Jausions@11abacus.com>
  31    * @author      Aidan Lister <aidan@php.net>
  32    * @version     $Revision: 1.1 $
  33    * @since       PHP 4.1.0
  34    * @require     PHP 4.0.1 (trigger_error)
  35    */
  36   if (!function_exists('version_compare')) {
  37  
  38       function version_compare ($version1, $version2, $operator = '<')
  39       {
  40           // Check input
  41           if (!is_scalar($version1)) {
  42               trigger_error('version_compare() expects parameter 1 to be string, ' . gettype($version1) . ' given', E_USER_WARNING);
  43               return null;
  44           }
  45  
  46           if (!is_scalar($version2)) {
  47               trigger_error('version_compare() expects parameter 2 to be string, ' . gettype($version2) . ' given', E_USER_WARNING);
  48               return null;
  49           }
  50  
  51           if (!is_scalar($operator)) {
  52               trigger_error('version_compare() expects parameter 3 to be string, ' . gettype($operator) . ' given', E_USER_WARNING);
  53               return null;
  54           }
  55  
  56           // Standardise versions
  57           $v1 = explode('.',
  58               str_replace('..', '.',
  59                   preg_replace('/([^0-9\.]+)/', '.$1.',
  60                       str_replace(array('-', '_', '+'), '.',
  61                           trim($version1)))));
  62  
  63           $v2 = explode('.',
  64               str_replace('..', '.',
  65                   preg_replace('/([^0-9\.]+)/', '.$1.',
  66                       str_replace(array('-', '_', '+'), '.',
  67                           trim($version2)))));
  68  
  69           // Replace empty entries at the start of the array
  70           while (empty($v1[0]) && array_shift($v1)) {}
  71           while (empty($v2[0]) && array_shift($v2)) {}
  72  
  73           // Describe our release states
  74           $versions = array(
  75               'dev'   => 0,
  76               'alpha' => 1,
  77               'a'     => 1,
  78               'beta'  => 2,
  79               'b'     => 2,
  80               'RC'    => 3,
  81               'pl'    => 4);
  82  
  83           // Loop through each segment in the version string
  84           $compare = 0;
  85           for ($i = 0, $x = min(count($v1), count($v2)); $i < $x; $i++)
  86           {
  87               if ($v1[$i] == $v2[$i]) {
  88                   continue;
  89               }
  90               if (is_numeric($v1[$i]) && is_numeric($v2[$i])) {
  91                   $compare = ($v1[$i] < $v2[$i]) ? -1 : 1;
  92               }
  93               elseif (is_numeric($v1[$i])) {
  94                   $compare = 1;
  95               }
  96               elseif (is_numeric($v2[$i])) {
  97                   $compare = -1;
  98               }
  99               elseif (isset($versions[$v1[$i]]) && isset($versions[$v2[$i]])) {
  100                   $compare = ($versions[$v1[$i]] < $versions[$v2[$i]]) ? -1 : 1;
  101               }
  102               else {
  103                   $compare = strcmp($v2[$i], $v1[$i]);
  104               }
  105  
  106               break;
  107           }
  108  
  109           // If previous loop didn't find anything, compare the "extra" segments
  110           if ($compare == 0) {
  111               if (count($v2) > count($v1))
  112               {
  113                   if (isset($versions[$v2[$i]])) {
  114                       $compare = ($versions[$v2[$i]] < 4) ? 1 : -1;
  115                   } else {
  116                       $compare = -1;
  117                   }
  118               }
  119               elseif (count($v2) < count($v1))
  120               {
  121                   if (isset($versions[$v1[$i]])) {
  122                       $compare = ($versions[$v1[$i]] < 4) ? -1 : 1;
  123                   } else {
  124                       $compare = 1;
  125                   }
  126               }
  127           }
  128  
  129           // Compare the versions
  130           if (func_num_args() > 2)
  131           {
  132               switch ($operator)
  133               {
  134                   case '>':
  135                   case 'gt':
  136                       return (bool) ($compare > 0);
  137                       break;
  138                   case '>=':
  139                   case 'ge':
  140                       return (bool) ($compare >= 0);
  141                       break;
  142                   case '<=':
  143                   case 'le':
  144                       return (bool) ($compare <= 0);
  145                       break;
  146                   case '==':
  147                   case '=':
  148                   case 'eq':
  149                       return (bool) ($compare == 0);
  150                       break;
  151                   case '<>':
  152                   case '!=':
  153                   case 'ne':
  154                       return (bool) ($compare != 0);
  155                       break;
  156                   case '':
  157                   case '<':
  158                   case 'lt':
  159                       return (bool) ($compare < 0);
  160                       break;
  161                   default:
  162                       return null;
  163               }
  164           }
  165  
  166           return $compare;
  167       }
  168   }
  169  
  170   ?>