Hi,
ich hab hier ne klasse geschrieben die die gültigkeit von isbn Nr. checkt. Fällt jemandem was auf das verbessert werden kann?
ich hab hier ne klasse geschrieben die die gültigkeit von isbn Nr. checkt. Fällt jemandem was auf das verbessert werden kann?
PHP-Code:
<?php
//////////////////////////////////////////////////////////
// ********** ISBN CLEARING *******************
//
// Class checkIsbn is checking the 10 and 13 digit ISBN
// numbers validity. The simplified input got checked by
// length and the rules for checksums for ISBN from
// http://www.pruefziffernberechnung.de
//
// usage:
// $valid = new checkIsbn("3-8155-0063-X");
// echo $valid->validIsbn();
//
//////////////////////////////////////////////////////////
class checkIsbn {
public $isbn;
// Get rid of anything except numbers
public function __construct($isbn) {
$this->isbn = ereg_replace("[^0-9X]","",$isbn);
}
public function validIsbn() {
//********** CHECK 13 digit ISBN ****************
if ( strlen($this->isbn) == 13 ) {
for ($i = 0; $i < strlen($this->isbn); $i++) {
$digitValue = substr ($this->isbn, $i, 1);
if ( $i%2 != 0 ) {
// odd numbers will be multiplicated with 3
$multi3[] = $digitValue;
} else {
// even numbers will sum up
$multi1[] = $digitValue;
}
}
// sum up odd numbers multiplicate with 3 - add sum of even numbers
// modulo out of 10 must be 0 to be valid
if ( ((array_sum($multi3)*3)+array_sum($multi1))%10 != 0 ) {
$this->isbnValid = false;
} else {
$this->isbnValid = true;
}
}
//********** CHECK 10 digit ISBN ****************
elseif ( strlen($this->isbn) == 10 ) {
// count down for weight of the number
// run only 9 digits 10th is checksum
$multiple = 10;
// run only 9 digits 10th is checksum last is checksum
for ($i = 0; $i < strlen($this->isbn)-1; $i++) {
$isbn10[] = substr ($this->isbn, $i, 1)*$multiple;
$multiple--;
}
// get checksum from input isbn
$checksum10 = substr(strtoupper($this->isbn), $i, 1);
// calculate checksum
$isbn10sum = 11-round(array_sum($isbn10)%11);
//special conditions in isbn rules
switch ( $isbn10sum ) {
case 10:
$isbn10sum = "X";
break;
case 11:
$isbn10sum = 0;
break;
default:
}
// status code of ISBN
if ( $isbn10sum == $checksum10 ) {
$this->isbnValid = true;
} else {
$this->isbnValid = false;
}
}
// if not 10 or 13 digit
else {
$this->isbnValid = false;
}
return $this->isbnValid;
}
}
Kommentar