Another RegExp question (extracting telephone numbers)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

Another RegExp question (extracting telephone numbers)

Post by visionmaster »

Hello together,

I'm trying to extract telephone numbers and have a "small" problem.

2 examples:
Telefon: 07128/12345 - 0 Telefax: 07128/12345
| |
------------------
|
07128123450

Telefon: ++49 (7128) 12345 - 0 - Fax: 07128/12345
| |
-------------------------
|
497128123450


Code: Select all

if (preg_match("/(telefon|tel\.|fon|telephone|telefonnr\.|telefonnummer)/i", $value)) 	
		{		
			$pattern = "/\D+/i";
			$replacement = "";
			$strTelefonnummer =	preg_replace($pattern, $replacement, $value);
			echo "<strong>Telefon&nbsp;&nbsp;&nbsp;:&nbsp; ".$strTelefonnummer."</strong><br>";

}


This would output me
071281234500712812345
=> Oops, not exactly what I want, since my pattern just takes all digits. It gives me telephone and telefax number. But I really just want the telephone number! How can I improve my RegExp?

Thanks a lot!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

you are doing a dumb replace.... (replace all non digits by '')


i think

preg_match("#^Telefon: (.*?) \w+ax: (.*?)$#", $string, $matches) will place the telephone and faxnumber in $matches[1] and $matches[2].

if you then preg_replace all non digits \D with '', you'll have the number ;)
visionmaster
Forum Contributor
Posts: 139
Joined: Wed Jul 14, 2004 4:06 am

Post by visionmaster »

timvw wrote: preg_match("#^Telefon: (.*?) \w+ax: (.*?)$#", $string, $matches)
[/quote]

Your solution works, thanks for your response!

Could you please explain a RegExp beginner in detail what happens, so I can understand 100%?

What does (.*?) do? I mean what does it match?
\w is clear, it stands for [a-zA-Z0-9_]
ax is actually perplexing me...

Thanks.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

.*? is non-greedy match for any character(s)
ax are the last letters of fax :)


a little websearch will probably get you some example where greedy matching (.*) actually matches more than you wanted ;)
Post Reply