Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php preg_replace phone numbers

Tags:

regex

php

I want to replace the given phone numbers in an html string, such as

<a>click here now! (123) -456-789</a>

I think that the best way to approach it would be to find all the different circumstances where there looks like a phone number, such as:

$pattern = *any 3 numbers* *any characters up to 3 characters long* 
$pattern .= *any 3 numbers* *any characters up to 3 characters long* 
$pattern .= *any numbers up to 4 numbers long*

// $pattern maybe something like [0-9]{3}\.?([0-9]{3})\.?([0-9]{4})

$array = preg_match_all($pattern, $string);

foreach($array)
{
    // replace the string with the the new phone number
}

Basically, how would the regex be?

like image 461
Jacob Kranz Avatar asked Dec 20 '22 03:12

Jacob Kranz


1 Answers

Based on the Local conventions for writing telephone numbers entry in Wikipedia, there are a variety of formats globally if you want to strip out ALL phone numbers. In the following examples the place holder 0 represents a number. The following is a sample from the wiki entry (there may be duplicates).

0 (000) 000-0000
0000 0000
00 00 00 00
00 000 000
00000000
00 00 00 00 00
+00 0 00 00 00 00
00000 000000
+00 0000 000000
(00000) 000000
+00 0000 000000
+00 (0000) 000000
00000-000000
00000/000000
000 0000
000-000-000
0 0000 00-00-00
(0 0000) 00-00-00
0 000 000-00-00
0 (000) 000-00-00
000 000 000
000 00 00 00
000 000 000
000 000 00 00
+00 00 000 00 00
0000 000 000
(000) 0000 0000
(00000) 00000
(0000) 000 0000
0000 000 0000
0000-000 0000
0000 000 0000
00000 000000
0000 000000
0000 000 00 00
+00 000 000 00 00
(000) 0000000
+00 00 00000000
000 000 000
+00-00000-00000
(0000) 0000 0000
+00 000 0000 0000
(0000) 0000 0000
+00 (00) 000 0000
+00 (0) 000 0000
+00 (000) 000 0000
(00000) 00-0000
(000) 000-000-0000
(000) [00]0-000-0000
(00000) 0000-0000
+ 000 0000 000000
8.8.8.8
192.168.1.1
0 (000) 000-0000 ext 1
0 (000) 000-0000 x 1001
0 (000) 000-0000 extension 2
0 000 000-0000 code 3

Since while you could try to write some crazy REGEX that would qualify each number based on it's country code, dialing prefix, etc for matching in your purposes this is not needed and would be a waste of time. From a Bayesian approach the longer numbers tend to be 18 characters (Argentina mobile numbers) with possibility of a leading + character followed by numbers [0-9] or \d, parenthesis (), brackets [] and possibly spaces , periods ., or hyphens - and one obscure format with a /.

\b\+?[0-9()\[\]./ -]{7,17}\b

For all of these numbers we'll also append the following extension formats

ext 123456
x 123456
# 123456
EXT 123456
- 123456
code 2
-12
Extension 123456

\b\+?[0-9()\[\]./ -]{7,17}\s+(extension|x|#|-|code|ext)\s+[0-9]{1,6}

So total you would look for phone numbers or phone numbers with extensions:

$pattern = '!(\b\+?[0-9()\[\]./ -]{7,17}\b|\b\+?[0-9()\[\]./ -]{7,17}\s+(extension|x|#|-|code|ext)\s+[0-9]{1,6})!i';

Note: that this will also strip IP addresses. If you want to keep IP addresses you will need to replace the periods in the IP addresses with something that will not match our Phone Number Regex, then switch them back.

So for your code you would use:

$string = preg_replace($pattern,'*Phone*',$string);

Here's a PHP fiddle of the matching test.

like image 86
AbsoluteƵERØ Avatar answered Jan 01 '23 03:01

AbsoluteƵERØ