After having consulted this page and started to draft a gambiarra, I noticed that I had in the "trunk" a function ready... And looking above I notice that the foundation has already been given here by @Danielomine, remembering that cell phone with DDD has 11 digits and fixed 10 digits. PS: no DDD are 9 and 8 respectively.
It was more or less in the year 2000 that Brazilian telephones started to have 8 digits, and between 2010 and 2013 that all cellulars started to have 9 digits. See anatel standards.
The generic algorithm of interpretation of a supposed telephone string expressed in free text is based on the Anatel standard... It’s just a little more complicated because it needs to give context and know how to differentiate cases with and without IDD and IDD.
To whom it may interest... Algorithm that "sanitizes" telephone data typed, written (in PL/SQL) and validated years ago... I’ll update if I find something better later (leaving now in Wiki for you to edit).
CREATE or replace FUNCTION lib.get_string_tel(text) RETURNS text AS $f$
SELECT regexp_replace(
$1
,'^.*?((\+?55[\s-]|\+\d\d[\s-])?\s*\(?\d\d\)?[\s\-\.]*\d[\.\s]?\d\d[\d\s\.\-]+).*?$'
,'\1'
)
$f$ LANGUAGE SQL IMMUTABLE;
CREATE or replace FUNCTION lib.telbr_split(
p_tel text -- a string contendo o telefone
,p_pode_ddi boolean DEFAULT true -- deixa preceder 55?
,p_ddd_default text DEFAULT '11' -- contexto Sampa
,p_max int DEFAULT 21 -- tamaho máximo da string para remover outros dígitos
) RETURNS text[] AS $f$
DECLARE
basenum text;
digits text;
len int;
ret text[];
BEGIN
basenum := trim(p_tel);
IF length(basenum)>p_max THEN
basenum := lib.get_string_tel(basenum);
END IF;
digits := lib.digits(basenum);
IF p_pode_ddi THEN
IF length(digits)>=14 THEN
basenum := regexp_replace(basenum,'^[^\d]*\d\d','');
digits := lib.digits(basenum);
ELSEIF length(digits)>=12 AND substr(digits,1,2)='55' THEN
basenum := regexp_replace(basenum,'^[^\d]*55','');
digits := lib.digits(basenum);
END IF;
END IF;
digits := regexp_replace(digits,'^0+','');
len := length(digits);
IF len<=9 THEN
digits := p_ddd_default||digits;
END IF;
RETURN array[substr(digits,1,2), substr(digits,3)];
END
$f$ LANGUAGE PLpgSQL IMMUTABLE;
The first function does extraction of the string apparently containing a phone number in the free text medium.
For example the other day registered Iphone 6s plus (11) 9 1234-5678
as phone number one of those Google forms.
The second is that it actually normalizes and explodes the number two parts, DDD and number.
The PL/SQL language (Postgresql or Oracle bases) is almost a Pascal, very easy to convert to PHP, Javascript, etc. Just have patience.
Normally I put only one mask, so the user is forced to type only numbers and in the format I need... http://digitalbush.com/projects/masked-input-plugin/
– user52919