正则表达式区分ISBN-10和ISBN-13 [英] regex differentiating between ISBN-10 and ISBN-13
问题描述
我有一条If-else语句,该语句检查字符串以查看是否有ISBN-10或ISBN-13(书ID).
I have an If-else statement which checks a string to see whether there is an ISBN-10 or ISBN-13 (book ID).
我面临的问题是在ISBN-13检查之前发生的ISBN-10检查,ISBN-10检查将与10个字符或更多字符匹配,因此可能将ISBN-13误认为是ISBN-10
The problem I am facing is with the ISBN-10 check which occurs before the ISBN-13 check, the ISBN-10 check will match anything with 10 characters or more and so may mistake an ISBN-13 for an ISBN-10.
这是代码...
$str = "ISBN:9780113411436";
if(preg_match("/\d{9}(?:\d|X)/", $str, $matches)){
echo "ISBN-10 FOUND\n";
//isbn returned will be 9780113411
return 0;
}
else if(preg_match("/\d{12}(?:\d|X)/", $str, $matches)){
echo "ISBN-13 FOUND\n";
//isbn returned will be 9780113411436
return 1;
}
如何确保避免出现此问题?
How do I make sure I avoid this problem?
推荐答案
为此,您实际上只需要一个正则表达式.然后,执行更有效的strlen()
检查以查看匹配的项.以下内容将匹配带或不带连字符的字符串中的ISBN-10和ISBN-13值,并可选地以字符串ISBN:
,ISBN:(space)
或ISBN(space)
开头.
You really only need one regex for this. Then do a more efficient strlen()
check to see which one was matched. The following will match ISBN-10 and ISBN-13 values within a string with or without hyphens, and optionally preceded by the string ISBN:
, ISBN:(space)
or ISBN(space)
.
function findIsbn($str)
{
$regex = '/\b(?:ISBN(?:: ?| ))?((?:97[89])?\d{9}[\dx])\b/i';
if (preg_match($regex, str_replace('-', '', $str), $matches)) {
return (10 === strlen($matches[1]))
? 1 // ISBN-10
: 2; // ISBN-13
}
return false; // No valid ISBN found
}
var_dump(findIsbn('ISBN:0-306-40615-2')); // return 1
var_dump(findIsbn('0-306-40615-2')); // return 1
var_dump(findIsbn('ISBN:0306406152')); // return 1
var_dump(findIsbn('0306406152')); // return 1
var_dump(findIsbn('ISBN:979-1-090-63607-1')); // return 2
var_dump(findIsbn('979-1-090-63607-1')); // return 2
var_dump(findIsbn('ISBN:9791090636071')); // return 2
var_dump(findIsbn('9791090636071')); // return 2
var_dump(findIsbn('ISBN:97811')); // return false
这将搜索提供的字符串,以查看它是否包含可能的ISBN-10值(返回1
)或ISBN-13值(返回2
).如果没有,它将返回false
.
This will search a provided string to see if it contains a possible ISBN-10 value (returns 1
) or an ISBN-13 value (returns 2
). If it does not it will return false
.
请参见上文的 演示 .
See DEMO of above.
对于 strict 验证,针对ISBN的Wikipedia 文章具有针对的一些PHP验证功能href ="https://en.wikipedia.org/wiki/International_Standard_Book_Number#Alternative_calculation"> ISBN-10 和
For strict validation the Wikipedia article for ISBN has some PHP validation functions for ISBN-10 and ISBN-13. Below are those examples copied, tidied up and modified to be used against a slightly modified version of the above function.
将返回块更改为此:
return (10 === strlen($matches[1]))
? isValidIsbn10($matches[1]) // ISBN-10
: isValidIsbn13($matches[1]); // ISBN-13
验证ISBN-10:
function isValidIsbn10($isbn)
{
$check = 0;
for ($i = 0; $i < 10; $i++) {
if ('x' === strtolower($isbn[$i])) {
$check += 10 * (10 - $i);
} elseif (is_numeric($isbn[$i])) {
$check += (int)$isbn[$i] * (10 - $i);
} else {
return false;
}
}
return (0 === ($check % 11)) ? 1 : false;
}
验证ISBN-13:
function isValidIsbn13($isbn)
{
$check = 0;
for ($i = 0; $i < 13; $i += 2) {
$check += (int)$isbn[$i];
}
for ($i = 1; $i < 12; $i += 2) {
$check += 3 * $isbn[$i];
}
return (0 === ($check % 10)) ? 2 : false;
}
请参见上文的 演示 .
See DEMO of above.
这篇关于正则表达式区分ISBN-10和ISBN-13的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!