Intervention\Validation\Validator::isIsbn PHP Method

isIsbn() public static method

Checks if given value is valid International Standard Book Number (ISBN).
public static isIsbn ( mixed $value ) : boolean
$value mixed
return boolean
    public static function isIsbn($value)
    {
        $value = str_replace(array(' ', '-', '.'), '', $value);
        $length = strlen($value);
        $checkdigit = substr($value, -1);
        if ($length == 10) {
            if (!is_numeric(substr($value, -10, 9))) {
                return false;
            }
            $checkdigit = !is_numeric($checkdigit) ? $checkdigit : strtoupper($checkdigit);
            $checkdigit = $checkdigit == 'X' ? '10' : $checkdigit;
            $sum = 0;
            for ($i = 0; $i < 9; $i++) {
                $sum = $sum + $value[$i] * (10 - $i);
            }
            $sum = $sum + $checkdigit;
            $mod = $sum % 11;
            return $mod == 0;
        } elseif ($length == 13) {
            $sum = 0;
            $sum = $value[0] + $value[1] * 3 + $value[2] + $value[3] * 3 + $value[4] + $value[5] * 3 + $value[6] + $value[7] * 3 + $value[8] + $value[9] * 3 + $value[10] + $value[11] * 3;
            $mod = $sum % 10;
            $correct_checkdigit = 10 - $mod;
            $correct_checkdigit = $correct_checkdigit == "10" ? "0" : $correct_checkdigit;
            return $checkdigit == $correct_checkdigit;
        }
        return false;
    }

Usage Example

 public function testValidateIsbn()
 {
     $isbn1 = '3498016709';
     $isbn2 = '978-3499255496';
     $isbn3 = '85-359-0277-5';
     $no_isbn = '123459181';
     $this->assertTrue(Validator::isIsbn($isbn1));
     $this->assertTrue(Validator::isIsbn($isbn2));
     $this->assertTrue(Validator::isIsbn($isbn3));
     $this->assertFalse(Validator::isIsbn($no_isbn));
 }