1 common.inc _backdrop_version_compare_convert($version_string)

Converts a Backdrop version string into numeric-only version string.

Parameters

string $version_string: A version string such as 1.x-1.2.3, 1.10.0-beta4, or 1.4.x-dev.

Return value

string: A converted string only containing numbers, for use in PHP's version_compare() function.

File

core/includes/common.inc, line 8874
Common functions that many Backdrop modules will need to reference.

Code

function _backdrop_version_compare_convert($version_string) {
  // Remove the "1.x-" prefix (indicating Backdrop core version compatibility).
  $core_prefix = BACKDROP_CORE_COMPATIBILITY . '-';
  if (strpos($version_string, $core_prefix) === 0) {
    $version_string = substr($version_string, strlen($core_prefix));
  }

  // Convert "dev" releases to be the highest possible version number. For
  // example 1.5.x-dev should be considered higher than any other 1.5 release,
  // so we replace .x with 99999.
  $version_string = str_replace('.x-dev', '.99999', $version_string);

  // Replace common indicators with numeric equivalents for version_compare().
  $indicator_order = array(
    1 => 'alpha',
    2 => 'beta',
    3 => 'preview',
    4 => 'rc',
  );
  foreach ($indicator_order as $order => $indicator) {
    // Replace both before and after the indicator with dots, so that multiple
    // alphas, betas, etc. would be ordered properly. For example, version
    // 1.5.0-beta9 and 1.5.0-beta10 would become 1.5.0.2.9 and 1.5.0.2.10.
    $version_string = str_replace('-' . $indicator, '.' . $order . '.', $version_string);
    $version_string .= substr($version_string, -1) === '.' ? '0' : '';
  }
  return $version_string;
}