2

working on a project right now where we have large amount of text strings that we must localize phone numbers and make them clickable for android phones.

The phone numbers can be in different formats, and have different text before and after them. Is there any easy way of detecting every kind of phone number format? Some library that can be used?

Phone numbers can show like, has to work with all these combinations. So that the outcome is like

<a href="tel:number">number</a>

+61 8 9000 7911

+2783 207 5008

+82-2-806-0001

+56 (2) 509 69 00

+44 (0)1625 500125

+1 (305)409 0703

031-704 98 00

+46 31 708 50 60

Kara
  • 6,115
  • 16
  • 50
  • 57
tobros91
  • 668
  • 1
  • 9
  • 24

4 Answers4

2

Perhaps something like this:

/(\+\d+)?\s*(\(\d+\))?([\s-]?\d+)+/
  • (\+\d+)? = A "+" followed by one or more digits (optional)
  • \s* = Any number of space characters (optional)
  • (\(\d+\))? = A "(" followed by one or more digits followed by ")" (optional)
  • ([\s-]?\d+)+ = One or more set of digits, optionally preceded by a space or dash

To be honest, though, I doubt that you'll find a one-expression-to-rule-them-all. Telephone numbers can be in so many different formats that it's probably impractical to match any possible format with no false positives or negatives.

kenorb
  • 155,785
  • 88
  • 678
  • 743
FtDRbwLXw6
  • 27,774
  • 13
  • 70
  • 107
  • will take spaces at start which are not part of the number, move spaces into the +int first optional part. also minimum requirements of numbers should be taken into account. – hakre Apr 13 '12 at 13:28
  • @hakre: I don't think it's possible to take minimum requirements of numbers into account, since the minimum is not consistent, and a pattern could meet the minimum and still be an invalid phone number. – FtDRbwLXw6 Apr 13 '12 at 13:36
  • If I read your regex right, you have a minimum requirement of a single digit. – hakre Apr 13 '12 at 13:38
  • @hakre: That's correct. What minimum do you suggest? At most, it would have to be 3 (411, 911, etc.). And to enforce a minimum, you'd have to make the regex much more complex. But what do you gain? Eliminating 1- and 2-digit false positives, I guess. I'll leave that trade-off up to the OP. IMO, it's not worth it. – FtDRbwLXw6 Apr 13 '12 at 14:44
  • well the minimum requirements could be checked in a callback function w/o putting too much drain on the regex itself. 3 digits minimum look good IMHO. – hakre Apr 13 '12 at 16:10
1

Not sure that there is a library for that. Hmmmm. Like any international amenities, telephone numbers are standardised and there should be a format defining telephone numbers as well. E.164 suggests recommended telephone numbers: http://en.wikipedia.org/wiki/E.164 . All open-source decoding libraries are built from reading these standard formats, so it should be of some help if you really cant find any existing libs

A Person
  • 1,350
  • 12
  • 23
0

I guess this might do it for these cases?

preg_replace("/(\+?[\d-\(\)\s]{7,}?\d)/", '<a href="tel:$1">number</a>', $str);

Basicly I check if it may start on +. It doesn't have to. Then I check if it got numbers, -, (, ) and spaces with at least 8 cases so it doesn't pick low non-phone numbers.

Robin Castlin
  • 10,956
  • 1
  • 28
  • 44
0

Try the following:

preg_match_all('/\+?[0-9][\d-\()-\s+]{5,12}[1-9]/', $text, $matches);

or:

preg_match_all('/(\+?[\d-\(\)\s]{8,20}[0-9]?\d)/', $text, $matches);
kenorb
  • 155,785
  • 88
  • 678
  • 743
shivanshu patel
  • 782
  • 10
  • 19