forked from mwhite/php-isbn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Isbn.php
122 lines (98 loc) · 3.37 KB
/
Isbn.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
class Isbn {
/**
* @param string $isbn a correctly-formatted ISBN10 or 13 (no hyphens)
* @return boolean whether $isbn is valid
*/
public static function validate($isbn) {
return self::validate10($isbn) || self::validate13($isbn);
}
/**
* @param string $isbn a correctly-formatted ISBN10 (no hyphens)
* @return boolean whether $isbn is valid
*/
public static function validate10($isbn) {
if (strlen($isbn) != 10 || !is_numeric(substr($isbn, 0, 9))) {
return false;
}
$sum = 0;
for ($i = 0; $i < 10; $i++) {
if ($isbn[$i] == 'X') {
$sum += 10 * (10 - $i);
} else if (is_numeric($isbn[$i])) {
$sum += $isbn[$i] * (10 - $i);
} else {
return false;
}
}
return $sum % 11 == 0;
}
/**
* @param string $isbn a correctly-formatted ISBN13 (no hyphens)
* @return boolean whether $isbn is valid
*/
public static function validate13($isbn) {
if (strlen($isbn) != 13 || !is_numeric($isbn)) {
return false;
}
$i = $isbn;
$sum = 3*($i[1] + $i[3] + $i[5] + $i[7] + $i[9] + $i[11])
+ $i[0] + $i[2] + $i[4] + $i[6] + $i[8] + $i[10];
return $i[12] == (10 - $sum % 10) % 10;
}
/**
* @param string $isbn an ISBN of any sort in any format
* @return false|string $isbn converted to an ISBN10, or false if $isbn is
* invalid or doesn't have an ISBN10 equivalent.
*/
public static function to10($isbn, $validate=false) {
$isbn = self::clean($isbn);
if ($validate && !self::validate($isbn)) {
return false;
}
if (strlen($isbn) == 10) {
return $isbn;
} else if (strlen($isbn) != 13 || substr($isbn, 0, 3) != '978') {
return false;
}
$i = substr($isbn, 3);
$sum = $i[0]*1 + $i[1]*2 + $i[2]*3 + $i[3]*4 + $i[4]*5
+ $i[5]*6 + $i[6]*7 + $i[7]*8 + $i[8]*9;
$check = $sum % 11;
if ($check == 10) {
$check = "X";
}
return substr($isbn, 3, 9) . $check;
}
/**
* @param string $isbn an ISBN of any sort in any format
* @return false|string $isbn converted to an ISBN13, or false if $isbn is
* invalid
*/
public static function to13($isbn, $validate=false) {
$isbn = self::clean($isbn);
if ($validate && !self::validate($isbn)) {
return false;
}
if (strlen($isbn) == 13) {
return $isbn;
} else if (strlen($isbn) != 10) {
return false;
}
$i = "978" . substr($isbn, 0, -1);
$sum = 3*($i[1] + $i[3] + $i[5] + $i[7] + $i[9] + $i[11])
+ $i[0] + $i[2] + $i[4] + $i[6] + $i[8] + $i[10];
$check = $sum % 10;
if ($check != 0) {
$check = 10 - $check;
}
return $i . $check;
}
/**
* @param string $isbn an ISBN in any format, including whitespace, hyphens, etc.
* @return string $isbn with all characters removed except numbers and 'X'
*/
public static function clean($isbn) {
return preg_replace("/[^0-9X]+/", '', $isbn);
}
}