Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I try to get phone numbers from string in german format. But I don't get it to full run. The input text is a full HTML-Page with lots of content, not only the numbers.

Possible Formats:

(06442) 3933023     
(02852) 5996-0       
(042) 1818 87 9919   
06442 / 3893023  
06442 / 38 93 02 3     
06442/3839023
042/ 88 17 890 0     
+49 221 549144 – 79  
+49 221 - 542194 79  
+49 (221) - 542944 79
0 52 22 - 9 50 93 10 
+49(0)121-79536 - 77 
+49(0)2221-39938-113 
+49 (0) 1739 906-44  
+49 (173) 1799 806-44
0173173990644
0214154914479
02141 54 91 44 79
01517953677
+491517953677
015777953677
02162 - 54 91 44 79
(02162) 54 91 44 79

I have tried:

$regex =  '~(?:+?49|0)(?:s*d{3}){2}s*d{4,10}~';
if(preg_match_all($regex, $input_imprint , $matches)){
    print_r($matches);
}

But it doesn't match only a few formats. I have no idea to do it.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
607 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here is a regex to match all your formats. I would suggest then to replace all unwanted characters and you got your desired result.

((?([d -)–+/(]+))?([ .-–/]?)([d]+))

If you need a minimum length to match your numbers, use this:

((?([d -)–+/(]+){6,})?([ .-–/]?)([d]+))

https://regex101.com/r/CAVex8/143

updated, thanks for the suggestion @Willi Mentzel


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...