Some small regular expression in its function, available configuration:
$number = '304-443-2456'; function mask_number($number, $count = 4, $seperators = '-') { $masked = preg_replace('/\d/', 'x', $number); $last = preg_match(sprintf('/([%s]?\d){%d}$/', preg_quote($seperators), $count), $number, $matches); if ($last) { list($clean) = $matches; $masked = substr($masked, 0, -strlen($clean)) . $clean; } return $masked; } echo mask_number($number);
If the function failed, it will return all masked ones (for example, another separator, less than 4 digits, etc.). In some cases, you can say about the safety of children.
Demo
hakre source share