I need help writing regular experssion that will match phone numbers in some data. Somin the following format:
XXX-XXX-XXXX
XXX-XXX-XXXX Ext. XXX
XXX-XXX-XXXX Ext. XXXX
(XXX) XXX-XXXX
XXX-XXX-XXXX (Text)
XXX.XXX.XXXX
and one regexp specifically for this line:
XXX-XXX-XXXX (Text & Special Chars.); XXX-XXX-XXXX (Text)
I’ve searched through several questions and tried the general 0-9 match but to no avail so far. I also tried this more recently to fetch normal phone numbers:
preg_match_all('/^((\(\d{3}\) ?)|(\d{3}-))?\d{3}-\d{4}$/', $a3, $n);
print_r($n);
Can someone show me the right way? Thanks
This pattern will match the examples you provided in any text.
((?:\d{3}[.-]|\(\d{3}\)\s+)\d{3}[.-]\d{4})(?:\s+Ext\.\s+(\d+)|\s+\(.*?\))?If you can provide some sample data or explain in more detail what the subject looks like the pattern can likely be improved. For example, are all the numbers on a separate lines (like your pattern would suggest) or are they part of a text?
Example script, and the pattern explained:
output:
and for the other line, the pattern above will also match that one, but if you do need it here it is
output: