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?

cwarn23 commented: Good Post! +12

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.

It could be completely any format like these for example:

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

That expression unfortunatly does not work

Well it does for one anyway

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)

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

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.

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

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

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 "<br><br>";
		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

Why not just use the following???

$str='sd;6sd,af98asd^f76asdf087%45098afsln&k257sdpk235';
$phone=preg_replace('@[^0-9]@','',$str);
echo $phone;

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

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

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>';
?>
commented: Perfect it was exactly what I was looking for! +1

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

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.