Basically, it preg_matchwill do just that:
$count = 0;
foreach($_POST as $k => $v)
{
if (preg_match('/\bchap1[^\d]{0,1}/', $k)) ++$count;
}
How the template works:
\b: word boundary. matches chap1, but not schap, it cannot be part of a larger stringchap1: ( \b, char, ,[^\d]{0,1}: , , . chap10 , chap1e
"" , :
$count = array();
foreach($_POST as $k => $v)
{
if (preg_match('/\bchap(\d+)(.*)/', $k, $match))
{
$match[2] = $match[2] ? $match[2] : 'empty';
if (!isset($count[$match[1]])) $count[$match[1]] = array();
$count[$match[1]][] = $match[2];
}
}
,
\bchap: , , wourd + literal(\d+): ( ). ($match[1])(.*): . , . , chap1 - , *
$count , :
array('1' => array('e1', 'a3'),
'10'=> array('s3')
);
$_POST :
array(
chap1e1 => ?,
chap1a3 => ?,
chap10s3=> ?
)
, , . , , - ""
$count = array();
foreach($_POST as $k => $v)
{
if (preg_match('/\bchap(\d+)/', $k, $match))
{
if (!isset($count[$match[1]])) $count[$match[1]] = array();
$count[$match[1]][$k] = $v;
}
}
: , ( ) (.*) .
, chap\d, :
echo 'count 1: ', isset($count[1]) ? count($count[1]) : 0, PHP_EOL;