正则表达式识别电话号码

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?

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

One fast and easy solution to all this cases is to create a temporal variable with only numbers.

I don't know any PHP, but in JS (you certainly can adapt it) it would be:

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

And then apply your regex to the aux variable.

A regex to match all your cases would be pretty ugly, but here I go:

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

And the word stuff, you can always do a:

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

(You can keep adding numbers to support, like ten, eleven, etc..) Also you can use a regexp to match the combinations of digits and strings. For example, modifying the one I used:

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

(I would really not recommend doing this)

Posting this for anyone who is looking for similar solution. With the help of CCH's answer above (accepted) and dquijada's, I came up with following function to hide contact number from the content.

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

FYI: This has only one issue, i.e., if there is a number mentioned in text format, it will get converted to the actual digit. For. e.g. if there is the following line:

This one is the very good case to solve.

The above line will get converted as follows:

This 1 is the very good case to solve.