954,580 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Extracting A Phone Number From A String With Other Numbers

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?

jonesa01
Newbie Poster
10 posts since Oct 2011
Reputation Points: 22
Solved Threads: 0
 

This is a regular expression that will match the 111 (111) 1112 number in the string. You can modify it to work with others.

$string = 'Phone 111 (111) 1112 to get this for 75% off - Call Now. Offer open to 5pm only full price after 5.';
if ( preg_match( '#([0-9]{3}\s*\([0-9]{3}\)\s*[0-9]{4})#',$string,$matches ) === 1 ) {
  //use matches array to get phone number
  //i think $matches[1] will be the phone number
}


That is an untested regex that I just did.

I am not sure if this is what you wanted, as your questions was kind of confusing and I read through it fast.

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

It could be completely any format like these for example:

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

jonesa01
Newbie Poster
10 posts since Oct 2011
Reputation Points: 22
Solved Threads: 0
 

That expression unfortunatly does not work

jonesa01
Newbie Poster
10 posts since Oct 2011
Reputation Points: 22
Solved Threads: 0
 

Well it does for one anyway

jonesa01
Newbie Poster
10 posts since Oct 2011
Reputation Points: 22
Solved Threads: 0
 

If you don't mind me asking, why are the phone numbers formatted that way? I have never seen phone numbers like that.

Anyway, you might have run multiple regular expressions.

#([0-9]{3}%[0-9]{3}%[0-9]{4})# //for 111%111%1112
#(\[[0-9]{2}\.[0-9]{1},[0-9]{2}-[0-9]{1}-[0-9]{2}\.[0-9]{2}\])# // for [11.1,11-1-11.12] (i do not think this one will work)
kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

I guess it is so they are not easily picked up when displayed on a page or something. I mean it is probably the case of removing the other numbers and take it from there i guess but doing that will be difficult. Thanks for your help

jonesa01
Newbie Poster
10 posts since Oct 2011
Reputation Points: 22
Solved Threads: 0
 

Are you wanting to get the individual numbers of each format and create a more readable phone number?

Your last post didn't make much sense to me.

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

Thats correct, the problem is the number could be split at any point by any symbol and with other numbers in the text this can be complicated

jonesa01
Newbie Poster
10 posts since Oct 2011
Reputation Points: 22
Solved Threads: 0
 

Can you post a more complete example of what you are parsing and the code you are doing it with?

kkeith29
Nearly a Posting Virtuoso
1,357 posts since Jun 2007
Reputation Points: 235
Solved Threads: 194
 

Well the problem is at the moment i dont have that much code, but this is an example of what i am parsing:

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

Or it could be like this:

n12# Phone [11.1,11-1-11.12] to get this for 75% off - Call Now. Offer open to 5pm only full price after 5.

Those are just two examples but the issue is the phone number could be formatted in a variety of different ways

The code so far only loads the text elements into an array and loops around them, what i need to do is extract that phone number and put it into the NNN-NNN-NNNN format

The code i currently have is:

<?php
	$d = file_get_contents("mike.txt");
	$m = explode("==============================================================================================",$d);
	foreach($m as $m1){
		echo $m1;
		echo "";
		if ( preg_match( '#(\[[0-9]{2}\.[0-9]{1},[0-9]{2}-[0-9]{1}-[0-9]{2}\.[0-9]{2}\])#',$m1,$matches ) === 1 ) {
			echo $matches[0];
			//print_r($matches);
		}
		echo "<hr>";
	}	
?>


What i need is more of a general regex tbat can handle just about any conbination. The only similarity is they are 10 numbers long

jonesa01
Newbie Poster
10 posts since Oct 2011
Reputation Points: 22
Solved Threads: 0
 

Why not just use the following???

$str='sd;6sd,af98asd^f76asdf087%45098afsln&k257sdpk235';
$phone=preg_replace('@[^0-9]@','',$str);
echo $phone;
cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

That will not work as there are other numbers in the string than the phone number

jonesa01
Newbie Poster
10 posts since Oct 2011
Reputation Points: 22
Solved Threads: 0
 

ow now I see. I'll work on that regex

cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

Try the following:

<?php
$string='Phone 111 (111) 1112 to get this for 75% off - Call Now. Offer open to 5pm only full price after 5.';
preg_match_all('@[^a-zA-Z]{7,}@is',$string,$matches);
$matches=$matches[0];
foreach ($matches AS $key=>$match) {
$matches[$key]=preg_replace('@[^0-9]@s','',$match);
}
echo '<xmp>';
print_r($matches);
echo '</xmp>';
?>
cwarn23
Occupation: Genius
Team Colleague
3,033 posts since Sep 2007
Reputation Points: 413
Solved Threads: 259
 

Thats perfect thank you, I couldn't find the way to do it thanks you have solved my problem!

jonesa01
Newbie Poster
10 posts since Oct 2011
Reputation Points: 22
Solved Threads: 0
 

And with a bit of additional error processing code I have it doing exactly what I want. Thank you all

jonesa01
Newbie Poster
10 posts since Oct 2011
Reputation Points: 22
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: