# Extracting a phone number from a string with other numbers

Page 1 of 1

## 6 Replies - 12373 Views - Last Post: 16 October 2011 - 01:45 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=251402&amp;s=52735f2cc87e6811a32902b672b98f04&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jonesa01

Reputation: 25
• Posts: 125
• Joined: 18-February 07

# Extracting a phone number from a string with other numbers

Posted 15 October 2011 - 06:40 AM

Hello,

I hope someone can help me solve this:

I am writing a script which can extract phone numbers from a peice of entered text which i can do fine but when there are other numbers it becomes more complex and i cannot seem to find away round it. All the numbers are not necessarily using the correct format, which i can easily sort once i have got the phone number. A few example numbers are:

111%111%1112
[11.1,11-1-11.12]
111 (111) 1112

An example of the text is:

Phone 111 (111) 1112 to get this for 75% off - Call Now. Offer open to 5pm only full price after 5.

Ofcourse some are longer and contain more numbers than that. Can anyone help me figure out how to fix this?

Is This A Good Question/Topic? 0

## Replies To: Extracting a phone number from a string with other numbers

### #2 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11794
• Posts: 44,316
• Joined: 27-December 08

## Re: Extracting a phone number from a string with other numbers

Posted 15 October 2011 - 08:34 AM

Are you familiar with Regular Expressions? You could build a regular expression to help you out with extracting the phone numbers.

### #3 jonesa01

Reputation: 25
• Posts: 125
• Joined: 18-February 07

## Re: Extracting a phone number from a string with other numbers

Posted 15 October 2011 - 08:35 AM

I am familiar but I am unsure how to do it in this case as they are all in different formats

macosxnerd101, on 15 October 2011 - 09:34 AM, said:

Are you familiar with Regular Expressions? You could build a regular expression to help you out with extracting the phone numbers.

### #4 jonesa01

Reputation: 25
• Posts: 125
• Joined: 18-February 07

## Re: Extracting a phone number from a string with other numbers

Posted 15 October 2011 - 09:22 AM

What I am wanting to find out is the regex needed to do this is someone can help

### #5 macosxnerd101

• Games, Graphs, and Auctions

Reputation: 11794
• Posts: 44,316
• Joined: 27-December 08

## Re: Extracting a phone number from a string with other numbers

Posted 15 October 2011 - 09:50 AM

I'm not that familiar with Regular Expressions. However, you may find supercorey's Regex Tutorial helpful.

### #6 jonesa01

Reputation: 25
• Posts: 125
• Joined: 18-February 07

## Re: Extracting a phone number from a string with other numbers

Posted 15 October 2011 - 11:23 AM

Still cannot seem to work it out, has anyone else got some suggestions?

This is what i have now

```<?php
\$d = file_get_contents("numbers.txt");
\$m = explode("==============================================================================================",\$d);
foreach(\$m as \$m1){
echo \$m1;
echo "<br><br>";
// This gets it down so it only displays the numbers and spaces
\$m2 = preg_replace("/[^0-9\s]/", "", \$m1);
echo \$m2."<br>";
// After a bit of searching this regex seems to be one to use but no result
preg_match_all('/\b[0-9]{3}\s*[-]?\s*[0-9]{3}\s*[-]?\s*[0-9]{4}\b/',\$m1,\$matches);
print_r(\$matches);
echo "<hr>";
}
?>

```

Any more suggestions?

### #7 jonesa01

Reputation: 25
• Posts: 125
• Joined: 18-February 07

## Re: Extracting a phone number from a string with other numbers

Posted 16 October 2011 - 01:45 AM

Thanks everyone for their suggestions, I have now found away of doing it and with a bit more validation have been able to get it working

Page 1 of 1

 .related ul{list-style-type:circle;font-size:12px;font-weight:bold;}.related li{margin-bottom:5px;background-position:left 7px!important;margin-left:-35px;}.related h2{font-size:18px;font-weight:bold;}.related a{color:blue;}