PHP fgetcsv中的正则表达式

I have a cycle in my code where I want to extract only the first, fifth and sixth number from an array that looks like this:

Array ( [0] => 12403;644;20;46;5;285;;;; )

I am currently trying to use regular expressions to extract those numbers (in this case I need to get 12403, 5 and 285).

$contents = fgetcsv($handle);
preg_match('/^(\d+);\d+;\d+;\d+;(\d+);(\d+);*.$/', $contents, $match);
echo "first capture group: " . $match[0] . "
second capture group: " . $match[1] . "
third capture group: " . $match[2];

This prints out empty variables. Why? Can you please help?

$contents is an array, preg_match needs a string. So it should be:

preg_match('/^(\d+);\d+;\d+;\d+;(\d+);(\d+);*.$/', $contents[0], $match);

Then, capture groups start at 1; $match[0] is the match for the entire regexp. So it should be:

echo "first capture group: " . $match[1] . "
second capture group: " . $match[2] . "
third capture group: " . $match[3];

But a simpler solution would be to use the correct delimiter when calling fgetcsv(), so it will split the line for you -- that's the whole point of using fgetcsv() instead of fgets().

$contents = fgetcsv($handle, ';');
echo "first capture group: " . $contents[0] . "
second capture group: " . $contents[4] . "
third capture group: " . $contents[5];