4

我想用 html 字符串替换给定的电话号码,例如

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

我认为解决它的最佳方法是找到看起来像电话号码的所有不同情况,例如:

$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
}

基本上,正则表达式会如何?

4

3 回答 3

10

根据Wikipedia 中用于编写电话号码条目的本地约定,如果您想删除所有电话号码,全球范围内有多种格式。在以下示例中,占位符0代表一个数字。以下是来自 wiki 条目的示例(可能有重复)。

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

因为虽然您可以尝试编写一些疯狂的 REGEX 来根据每个号码的国家代码、拨号前缀等来限定每个号码以匹配您的目的,但这不是必需的,而且会浪费时间。从贝叶斯方法来看,较长的数字往往是 18 个字符(阿根廷手机号码),前导+字符可能后跟数字[0-9]\d,括号(),括号[],可能还有空格,句点.或连字符-,以及一种带有/.

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

对于所有这些数字,我们还将附加以下扩展格式

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}

因此,您总共需要查找电话号码或带有扩展名的电话号码:

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

注意:这也会剥离 IP 地址。如果您想保留 IP 地址,您需要将 IP 地址中的句点替换为与我们的电话号码正则表达式不匹配的内容,然后将它们切换回来。

因此,对于您的代码,您将使用:

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

这是匹配测试的 PHP 小提琴

于 2013-06-27T15:25:38.260 回答
1

我认为这将匹配两组三位数和一组四位数,中间有“通用”电话号码标点符号:

\d{3}[().-\s[\]]*\d{3}[().-\s[\]]*\d{4}

这允许三位数字,然后是任意数量的标点符号或空格,然后是三位数字,然后是更多标点符号,然后是四位数字。

但是,如果对输入的格式没有更好的了解,您将永远无法真正确定您将获得电话号码而不是其他任何东西,或者您不会跳过任何电话号码。

如果你想用你自己的号码替换你找到的号码,我可能会尝试这样的事情:

preg_replace('/\d{3}([().-\s[\]]*)\d{3}([().-\s[\]]*)\d{4}/',
    "123$1456$27890", $input);

在替换字符串中,$1$2是数字之间的两个带括号的标点符号块。这样,您可以只替换您找到的数字,并通过将相同的标点符号重新插入结果字符串来保留标点符号。

于 2013-06-26T22:19:52.550 回答
0

这是我从某个地方下载的函数(不记得我从哪里得到的)。

/*
// PHP function to validate US phone number:
// (c) 2003
// No restrictions have been placed on the use of this code
//
// Updated Friday Jan 9 2004 to optionally ignore the area code:
//
// Input: a single string parameter and an optional boolean variable (default=true)
// Output: 10 digit telephone number or boolean false(0)
//
// The function will return the numerical part of the alphanumeric string
// parameter with the following sequence of characters:
// any number of spaces [optional],
// a single open parentheses [optional],
// any number of spaces [optional],
// 3 digits (area code),
// any number of spaces [optional],
// a single close parentheses [optional],
// a single dash [optional],
// any number of spaces [optional],
// 3 digits, any number of spaces [optional],
// a single dash [optional],
// any number of spaces [optional],
// 4 digits, any number of spaces [optional]:
*/
function validate_USphone($phonenumber, $useareacode=true)
{
   if ( preg_match("/^[ ]*[(]{0,1}[ ]*[0-9]{3,3}[ ]*[)]{0,1}[-]{0,1}[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/",$phonenumber) || (preg_match("/^[ ]*[0-9]{3,3}[ ]*[-]{0,1}[ ]*[0-9]{4,4}[ ]*$/",$phonenumber) && !$useareacode)) return preg_replace("/[^0-9]/i", "", $phonenumber);
   return false;
}
于 2013-06-26T22:23:07.460 回答