Page 1 of 1

Another RegExp question (extracting telephone numbers)

Posted: Mon Jan 24, 2005 3:22 pm
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!

Posted: Mon Jan 24, 2005 5:31 pm
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 ;)

Posted: Tue Jan 25, 2005 2:30 am
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.

Posted: Tue Jan 25, 2005 9:15 am
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 ;)