$re = '/^[\s\S]+?(?<!\d)(\d{8})(?!\d)/';
$str = 'Test123456789 -- should fail because 9 digits
Test23456789Test -- pass
Test23456789 Test -- pass
13456780Test -- pass
Test0123456 -- fail because 7 digits
Extra text in the email: I’ve attached the information you requested. If you have any questions, please let us know. -- extra text in the email shouldn\'t matter.';
preg_match($re, $str, $matches, PREG_OFFSET_CAPTURE, 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