duanpo8329 2017-02-16 12:53
浏览 46
已采纳

正则表达式识别电话号码

I have a requirement where I have to hide phone number in messages provided by users. I already have one regular expression which is as follows:

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

But this can only identify mobile numbers of following formats:

9876543210

I want it to cover following formats too:

987 654 3210

9 8 7 6 5 4 3 2 1 0

(987) 654 3210

(987) (654) (3210)

In all the above formats, spaces can be replaced by either '-' or '.'. Also, '(' and ')' can be replaced by '[' and ']'.

Also, is it possible to identify phone numbers which are mentioned with strings instead of digits, like

Nine eight seven six five four three two one zero

Any combination of digits and strings

EDIT: Adding my function which is hiding contact numbers if any from content:

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;

}

The above function does not identify and hide all the number sequences I have mentioned. Even @CCH's solution does not help. Is anything wrong with this function?

  • 写回答

3条回答 默认 最新

  • dongshou9878 2017-02-21 16:59
    关注

    This :

    [\([]?([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])

    will match all your examples.
    Demo here :
    https://regex101.com/r/h9631Z/4

    For a full php function, use this :

    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);
    }
    

    You can change $subst to set what it will replace the matches to.

    Full demo here : https://repl.it/FnSp/3

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思