Capture all phones from a text file

1

I have a log records in html, and would like to capture all the landline and mobile numbers that are scattered around the file, I use the following regular expression currently: /(\(?\d{2}\)?) ?9?\d{4}-?\d{4}/ , only it is letting pass some numbers that not phones, what's wrong?

preg_match_all('/(\(?\d{2}\)?) ?9?\d{4}-?\d{4}/', $phone, $out);

echo '<pre/>';
print_r(array_shift($out));

number passed: 99988877766

    
asked by anonymous 13.06.2017 / 05:46

1 answer

1

Depends on the pattern of numbers

An example regular expression:

/(?:\((\d{2})\))?\s?(\d{3,5}[-\s]?\d{4})/

It will capture, in group 1 the DDD and in group 2 the number, including dashes or spaces.

Patterns it captures:

(11)1234-0000
11-123-4567
11-1234567
(11)1234-1234
(11)123-1203
(11)12349-0293
1234-2039
99888-9292

To get more correct in the capture you must first seek all the possibilities of formats that you have in your file. For example, this expression does not capture numbers that have the international dialing code.

To test the expressions you can use PHP Live Regex . It allows you to do the tests online.

    
13.06.2017 / 13:50