/    Sign up×
Community /Pin to ProfileBookmark

extract all phone numbers from a text

Hi All ,

I want to extract all phone numbers from a text including all phone number formats.
I mean the code must extract :
01119799611
00201119799611
0111 97 99 611
002-01119799611
(002)01119799611
0111.97.99.611

Can Someone help ?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@NogDogMar 11.2013 — Probably you'd use preg_match_all(), but first you'll need to define all possible patterns you might want to search for, and convert them into regular expressions -- or possibly one regexp if you're lucky (or good).
Copy linkTweet thisAlerts:
@chrisranjanaMar 11.2013 — Hi All ,

I want to extract all phone numbers from a text including all phone number formats.

Can Someone help ?[/QUOTE]


You can start by learning more about regexp from places like (http://www.tutorialspoint.com/php/php_regular_expression.htm) and then move on to online regexp testing websites like (http://gskinner.com/RegExr/).
Copy linkTweet thisAlerts:
@darroosh2authorMar 11.2013 — I know that it can be done using regular expressions . But i'm pretty new to regex , So I need help creating its regex.

I think the searched value must be : zero or more occurances of ( any number or dash or space or () or plus ) then must be : ( 01 ) then more than 9 occurances of (any number or dash or space ).
Copy linkTweet thisAlerts:
@darroosh2authorMar 11.2013 — Thank you for your time.

I think I did it :

$search = preg_match_all("/(d+|-|+|(|)| ){0,}(01)(d+| |-){8,14}/",$text,$matches);

This regex worked fine and returned all phone number but noticed two problems:

1- if two mobile number are written beside each others , they are extracted as one number, for example: +2 0111 9799 - 618 +2 0111 9799 - 618


2- The following is matched : "2001 - 2006 "

Notice the spaces after 2006 , So How can I make it only one space ?

And how can force it to stop when it finds "plus" or when it finds "tab" or "more than one space"

Regards,
Copy linkTweet thisAlerts:
@SebastianGrayNov 06.2020 — @darroosh2#1254633 dude, there are a lot of services on the Internet where you can extract numbers from text and information about these numbers
Copy linkTweet thisAlerts:
@SebastianGrayNov 08.2020 — I know a good generator that can use regular expressions to select all phone numbers from a large text. But there are a lot of them on the Internet now, so you just need to search correctly. Everything is quite simple: you enter your text in the text block, and the program at the bottom will write you all the phone numbers that are written in this text. In General, I do not like this whole topic concerning phone numbers because in our time there are so many bad people who want to profit from the problems of bad people. In General, there are many scenarios, you can get a call and say that your card is blocked, yell and call and throw the phone. You will have to call back to resolve all these issues but you will be transferred to a paid line .Usually, money is withdrawn from your mobile account after you get through. And it is difficult to dispute – they themselves dialed this number from their phone. I fell for that trick, too. I got a call and got pissed off, and then hung up, and since I was furious, I decided to call back and deal with everything that was going on. As a result, I was charged $245. I decided to check the number in the spam database [256 510 2225](https://calldetective.net/numbers/256-510-2225/) and was not even surprised when I found it in the spam database and my guesses were confirmed.
Copy linkTweet thisAlerts:
@NogDogNov 08.2020 — {"locked":true}
×

Success!

Help @darroosh2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...