Using
preg_match_all('/(\d{4})/', $text, $matches); return $matches;
No need to use a character class if you only have \d to match (I skipped the square brackets).
If you want to match either a 4-digit or a 2-digit number, use
preg_match_all('/(?<!\d)(\d{4}|\d{2})(?!\d)/', $text, $matches); return $matches;
Here I use a negative lookbehind (?<!\d) and a negative lookahead (?!\d) to prevent the 2-digit parts of the three-digit numbers from matching (for example, to prevent 123 from matching as 12 ).
source share