It requires a bit more than just regex, but this worked for me (note that it finds the first "items" string and then checks if it's valid, if an error is found it returns 0 as the page count).
If you happen to want to match "items" string that might contain only one number/range you will need to identify what's bordering the "items" string because currently this method requires at least 2 numbers/ranges separated by commas.
<?php
function countpages($string) {
if (preg_match('/(?:(?:\d+-\d+|\d+),)+(?:\d+-\d+|\d+)/',$string,$result)) {
$checkone=preg_split('/,|-/',$result[0]);
for ($h = 0; $h < count($checkone)-1; $h++) {
if ($checkone[$h]>=$checkone[$h+1]) {
return 0;
}
}
preg_match_all('/(\d+)-(\d+)/',$result[0],$ranges);
$count=count($checkone)-(count($ranges[0])*2);
for ($i = 0; $i < count($ranges[0]); $i++) {
$count=$count+($ranges[2][$i]-$ranges[1][$i]+1);
}
return $count;
} else {
return 0;
}
}
echo '<pre>';
echo '1. pages:'.countpages(' 1,3,5-6 : Match (4 items)').'<br>';
echo '2. pages:'.countpages('1,5,10-12,15,25-30,55 : Match (13 items)').'<br>';
echo '3. pages:'.countpages('1,5-4,7 : No Match (not ascending order)').'<br>';
echo '4. pages:'.countpages('Any other caracter but 0123456789,- no match').'<br>';
echo '5. pages:'.countpages(' 3,3-5 : No Match (duplicates)').'<br>';
?>