繁体   English   中英

正则表达式识别电话号码

[英]Regex to identify phone number

我有一个必须在用户提供的消息中隐藏电话号码的要求。 我已经有一个正则表达式,如下所示:

/\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})/

但这只能识别以下格式的手机号码:

9876543210

我也希望它涵盖以下格式:

987 654 3210

9 8 7 6 5 4 3 2 1 0

(987)654 3210

(987)(654)(3210)

在上述所有格式中,空格都可以用“-”或“。”代替。 同样,'('和')'可以替换为'['和']'。

另外,是否可以识别用字符串而不是数字提及的电话号码,例如

九八七六五四有三二一零

数字和字符串的任意组合

编辑:添加我的功能是隐藏联系人号码,如果有的话从内容:

function hide_contact_number($description) {
// Find contact number and hide it!
$regex = "/\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})/";
/*$regex = "/[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{4})[\)\]]?|([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])/";*/
if(preg_match_all($regex, $description, $matches, PREG_OFFSET_CAPTURE)) {
    foreach($matches as $matchkey => $match) {
        foreach($match as $key => $value) {
            $index = 0;
            $length = 0;
            if(is_array($value)) {
                if(is_numeric($value[0]) && strlen($value[0]) >= 10) {
                    $index = $value[1];
                    $length = strlen($value[0]);
                } else if(strlen($value[1]) >= 10) {
                    $index = $value[0];
                    $length = strlen($value[1]);
                } else {
                    // TODO: Do nothing
                }
            }

            if($length > 0) {
                // length - 2 => 2 places before end of email id including 1 of index + 1
                $description = substr_replace($description, str_repeat("*", $length-2), $index+1, $length-2);
            }
        }
    }
}

return $description;

}

上面的函数不能识别并隐藏我提到的所有数字序列。 甚至@CCH的解决方案也无济于事。 这个功能有什么问题吗?

这个 :

[\\([]?([0-9]{3})[\\)\\]]?[-. ]?[\\([]?([0-9]{3})[\\)\\]]?[-. ]?[\\([]?([0-9]{4})[\\)\\]]?|([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])

将匹配您的所有示例。
演示在这里:
https://regex101.com/r/h9631Z/4

要获得完整的php功能,请使用此命令:

function hide_contact_number($description) {
$re = '/[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{4})[\)\]]?|([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])[-. ]([0-9])/';
$subst = '*** *** ***';
return preg_replace($re, $subst, $description);
}

您可以更改$ subst来设置它将替换匹配项的内容。

完整的演示在这里: https : //repl.it/FnSp/3

所有这些情况的一种快速简便的解决方案是创建仅包含数字的时间变量。

我不知道任何PHP,但是在JS(您当然可以修改)中,它将是:

aux = string.replace(/\D/g, '')

然后将您的正则表达式应用于aux变量。

一个可以满足您所有情况的正则表达式会很丑陋,但是我在这里:

\(?\d\s*\d\s*\d\)\s*\(?\d\s*\d\s*\d\)\s*\(?\d\s*\d\s*\d\s*\d)

和“东西”一词一样,您始终可以执行以下操作:

number = string
    .replace(/one/g, '1')
    .replace(/two/g, '2')
    .replace(/three/g, '3')
    .replace(/four/g, '4')
    .replace(/five/g, '5')
    .replace(/six/g, '6')
    .replace(/seven/g, '7')
    .replace(/eight/g, '8')
    .replace(/nine/g, '9')
    .replace(/zero/g, '0');

(您可以继续添加数字以供支持,例如10、11等。)您还可以使用正则表达式来匹配数字和字符串的组合。 例如,修改我使用的那个:

\(?d|one|two|three|four|five|six|seven|eight|nine\s*d|one|two|three|four|five|six|seven|eight|nine\s*d|one|two|three|four|five|six|seven|eight|nine\)?\s*\(?d|one|two|three|four|five|six|seven|eight|nine\s*d|one|two|three|four|five|six|seven|eight|nine\s*d|one|two|three|four|five|six|seven|eight|nine\)?\s*\(?d|one|two|three|four|five|six|seven|eight|nine\s*d|one|two|three|four|five|six|seven|eight|nine\s*d|one|two|three|four|five|six|seven|eight|nine\s*d|one|two|three|four|five|six|seven|eight|nine\)?

(我真的不建议这样做)

将其发布给正在寻找类似解决方案的任何人。 借助CCH上面(已接受)的答案和dquijada的答案,我想出了以下功能,可从内容中隐藏联系人号码。

function hide_contact_number($description) {
    $search = array('zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine');
    $replace = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
    $description = str_ireplace($search, $replace, $description);

    $regex = '/[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{3})[\)\]]?[-. ]?[\([]?([0-9]{4})[\)\]]?' .
    '|([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*([0-9])[-. ]*/';
    $description = preg_replace($regex, str_repeat('*', 10), $description);

    return $description;
}

仅供参考:这只有一个问题,即,如果以文本格式提及数字,它将转换为实际数字。 对于。 例如,如果有以下行:

This one is the very good case to solve.

上面的行将被转换如下:

This 1 is the very good case to solve.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM