1

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?

5
  • You should first try to understand a bit more what the regexp you have is doing. It matches more than just 9876543210, but also : 987 654 3210 or 987-654-3210 or 987.654.3210. Have a look at the website regex101.com that will help you understand better how it works. Commented Feb 16, 2017 at 13:32
  • Yes I understand that, but in reality, when I try to run '987 654 3210' or '987-654-3210' or '987.654.3210', it does not work. Commented Feb 21, 2017 at 11:54
  • Then you'll have to provide some more code on what is not working, because as you can see here : regex101.com/r/h9631Z/2 your regexp matches the 3 cases above. Commented Feb 21, 2017 at 16:50
  • Have added my function which identifies numbers and hide them. Please check. Commented Feb 22, 2017 at 8:29
  • You made my eyes bleed :) See my updated answer, preg_replace is enough to replace matches. Commented Feb 22, 2017 at 15:06

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I tried your solution, though it works on demo, when I include it in my function it does not identify all the cases mentioned. Please check my function in edit
1

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.

Comments

0

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.