I have a regex pattern I found for removing phone numbers from strings of text. It works great, except a couple of cases (these are US phone numbers).
Here is the regex:
/\(?\d{3}\)?[-\s.]?\d{3}[-\s.]\d{4}/x
Here are the cases I need to catch:
- 55555555555 (area code + 7 numbers)
- 155555555555 (1 + area code + 7 numbers)
- (555)-5555555 (area code in parenthesis, dash, 7 numbers)
- 1-555-555-5555
- 1-(555)-555-5555
Here is the regex replace I am using:
$pattern = "/\(?\d{3}\)?[-\s.]?\d{3}[-\s.]\d{4}/x";
$replacement = "[phone redacted]";
$body = preg_replace($pattern, $replacement, $body);