$re = '/^9?[\d]{4}-[\d]{4}$/m';
$str = '# 8 dígitos sem traço
12345678
92841742
83457854
# 9 dígitos sem traço
123457890
899879878
983457854
912345678
# 8 dígitos com traço
1234-5678
9284-1742
8345-7854
# 9 dígitos com traço
12345-7890
89987-9878
98345-7854
91234-5678';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php