You are here

function generate_ISN_corrections in ISBN Field 6

2 calls to generate_ISN_corrections()
generate_ISBN_corrections in ./isbn.inc
generate_ISSN_corrections in ./isbn.inc

File

./isbn.inc, line 451

Code

function generate_ISN_corrections($ISN_proto, $length) {
  $corrections = array();
  $ISN_proto = ISN_clean($ISN_proto);
  if (strlen($ISN_proto) == $length - 1) {

    // First try possible insertions in the first $length-1 positions.
    for ($pos = 0; $pos < $length - 1; $pos++) {
      for ($i = 0; $i <= 9; $i++) {
        $ISN_new = substr($ISN_proto, 0, $pos) . (string) $i . substr($ISN_proto, $pos);
        if (ISN_checksum_OK($ISN_new, $length)) {
          $corrections[canonical_ISN($ISN_new, $length)] = true;
        }
      }
    }
    if ($ISN_proto[$length - 2] != 'X') {
      $ISN_new = $ISN_proto . make_checkdigit(mod11_checksum($ISN_proto, $length));
      $corrections[canonical_ISN($ISN_new, $length)] = true;
    }
  }
  elseif (strlen($ISN_proto) == $length) {

    // Try replacements and interchanges for first nine positions.
    for ($pos = 0; $pos < $length - 1; $pos++) {
      $current = $ISN_proto[$pos];

      // try all possible replacements
      for ($i = 0; $i <= 9; $i++) {
        $ISN_proto[$pos] = (string) $i;
        if (ISN_checksum_OK($ISN_proto, $length)) {
          $corrections[canonical_ISN($ISN_proto, $length)] = true;
        }
      }

      // try the next character interchange (unless there is a final "X").
      $ISN_proto[$pos] = $ISN_proto[$pos + 1];
      $ISN_proto[$pos + 1] = $current;
      if ($ISN_proto[$pos] != 'X' && ISN_checksum_OK($ISN_proto, $length)) {
        $corrections[canonical_ISN($ISN_proto, $length)] = true;
      }

      // reset to the original values before moving on to the next position.
      $ISN_proto[$pos + 1] = $ISN_proto[$pos];
      $ISN_proto[$pos] = $current;
    }

    // Now replace the check digit with the correct value.
    $ISN_proto[$length - 1] = make_checkdigit(mod11_checksum(substr($ISN_proto, 0, $length - 1), $length));
    if (bad_ISN_char_count($ISN_proto) == 0) {
      $corrections[canonical_ISN($ISN_proto, $length)] = true;
    }
  }
  elseif (strlen($ISN_proto) == $length + 1) {
    for ($pos = 0; $pos <= $length; $pos++) {
      $ISN_new = substr($ISN_proto, 0, $pos) . substr($ISN_proto, $pos + 1);
      if (ISN_checksum_OK($ISN_new, $length)) {
        $corrections[canonical_ISN($ISN_new, $length)] = true;
      }
    }
  }
  return $corrections;
}