簡體   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