Jump to content

Recommended Posts

Let me explain what I am trying to do.

I'm trying to extract all phone numbers from a form of data, and it should result in an array of all phone numbers within the text.

So var I managed absolutely nothing, I've been trying to write a regex pattern for this for quite a while and can not seem to make anything work.

I'm reading off of a cheat sheet as I do not know so much about regex in total, except for finding simple things.

 

What I currently have uttered up is the following expression:

/\+*(?=[1-999])|(!?=0){7,9}[0-9]/

It doesn't even find anything, AT ALL.

I'm testing with the following string:

asd+27845513786 asd +98765433 asd

It should return the two phone numbers, but doesn't.

 

The rules I've laid out for it are:

1. Look for the international symbol, indicating it's an international dialing number with a valid extension(from +1 to +999).

2. If the plus symbol is present, make sure the next following character is a number.

3. If there is none, look at the length to validate it is between 7 and 10 digits long.

4. In the event that the number is divided (correctly via international standers) by either a hyphen(-) or space make sure the amount of digits in between them are either 3 or 4.

 

It sounds so simple, but my methodologies have not succeeded thus var.

If someone could give me advice on this matter it would greatly be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/216398-extracting-phone-numbers/
Share on other sites

Thanks for your help, although it's not exactly what I need.

I a large set of data it just wouldn't work.

 

I've been playing around with it some more, and actually have something that works but not as I hoped.

It is getting the phone numbers correctly, international and local ones but it doesn't pick up hyphens or spaces. I think I'm just making life harder for myself.

 

I've now decided to maybe run two regex searches. one to pick up international phone numbers and one for local ones.

Just one thing with the international ones, it is not including the + symbol in the results: why is that?

 

International search:

\+(?=[1-999])(\d{4}[0-9][-\s]\d{3}[0-9][-\s]\d{4}[0-9])|(\d{7,11}[0-9])

International search shorter(includes the symbol):

\+(?=[1-999])\d{7,11}

local:

\d{7,10}[0-9]

 

Thanks again.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.