Beecause ereg won't match one pattern to the whole string. And ereg returns an array with FALSE as it's only value, so when you count it, it gives you 1; when in reality it's not working. What you'd probably want is the PERL compatible function [man]preg_match_all/man. Then you'd have to change your pattern to be:
"~([0-9]*)~"
Ereg only finds whole string patterns, but preg can. So your code:
$input = "maths 89 c++ 92 java 85 uml 80";
preg_match_all("~([0-9]*)~",$input,$regs);
echo "Scores:<br>";
echo "num: " . count($regs);
for($i=0;$i<count($regs);$i++){
echo "$regs[$i] ";
}
Now, it would seem you might want something a little more powerful like this:
preg_match_all("~([a-z\+\s]*)([0-9]*)~", $input, $regs);
array_shift($regs);
echo 'Scores:<br>';
echo 'num: '.(count($regs[0])-1).'<br>';
for($i=0; $i<count($regs[0]); $i++)
{
if(!empty($regs[0][$i]) && !empty($regs[1][$i]))
{
echo $regs[0][$i].' '.$regs[1][$i].'<br>';
}
}
which outputs:
Scores:
num: 4
maths 89
c++ 92
java 85
uml 80