6 Replies - 8419 Views - Last Post: 16 October 2011 - 01:45 AM Rate Topic: -----

#1 jonesa01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 25
  • View blog
  • 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  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10804
  • View blog
  • Posts: 40,278
  • 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.
Was This Post Helpful? 0
  • +
  • -

#3 jonesa01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 25
  • View blog
  • 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

View Postmacosxnerd101, 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.

Was This Post Helpful? 0
  • +
  • -

#4 jonesa01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 25
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

#5 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10804
  • View blog
  • Posts: 40,278
  • 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.
Was This Post Helpful? 0
  • +
  • -

#6 jonesa01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 25
  • View blog
  • 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?
Was This Post Helpful? 0
  • +
  • -

#7 jonesa01  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 25
  • View blog
  • 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
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1