在電商訂單、用戶中心、評價列表等場景,為保護用戶隱私,需對敏感信息進行脫敏展示。常見規則:
j***n)138****8888)123***@qq.com)<?php
/**
* 敏感信息星號遮罩
* @param string $str 原始字符串
* @return string 脫敏後字符串
*/
function hideStar($str) {
// 郵箱處理
if (strpos($str, '@') !== false) {
$email_array = explode("@", $str);
$prefix = $email_array[0];
$domain = $email_array[1];
// 前綴長度小於 4 直接全遮罩,否則保留前 3 位
$prevfix = (strlen($prefix) < 4) ? "" : substr($prefix, 0, 3);
$masked_prefix = $prevfix . "***";
return $masked_prefix . "@" . $domain;
}
// 手機號處理(支援 11 位主流號段)
$pattern = '/(1[3-9]\d{1})\d{4}(\d{4})/';
if (preg_match($pattern, $str)) {
return preg_replace($pattern, '$1****$2', $str);
}
// 通用字符串:保留首尾,中間遮罩
$len = strlen($str);
if ($len <= 4) {
return str_repeat('*', $len);
}
return substr($str, 0, 2) . "***" . substr($str, -1);
}
// 測試示例
$account = "jb51.net";
$email = "123456789@qq.com";
$phone = "13888888888";
echo "賬號: " . hideStar($account) . "<br>"; // jb***t
echo "郵箱: " . hideStar($email) . "<br>"; // 123***@qq.com
echo "手機: " . hideStar($phone) . "<br>"; // 138****8888
?>
<!DOCTYPE html>
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP 敏感信息星號遮罩演示</title>
<style>
.table_parameters { width: 100%; border-collapse: collapse; margin-top: 20px; }
.table_parameters td, .table_parameters th { border: 1px solid #ddd; padding: 10px; text-align: center; }
.tr_head { background: #f5f5f5; font-weight: bold; }
.red { color: #e74c3c; font-weight: bold; }
</style>
</head>
<body>
<div class="container">
<h2>敏感信息脫敏展示對比</h2>
<table class="table_parameters">
<tr class="tr_head">
<td>原始賬號</td>
<td>原始郵箱</td>
<td>原始手機</td>
</tr>
<tr>
<td><?php echo $account; ?></td>
<td><?php echo $email; ?></td>
<td><?php echo $phone; ?></td>
</tr>
<tr class="red">
<td><?php echo hideStar($account); ?></td>
<td><?php echo hideStar($email); ?></td>
<td><?php echo hideStar($phone); ?></td>
</tr>
</table>
</div>
</body>
</html>
class DataMasker {
private $masks = [
'mobile' => ['pattern' => '/(1[3-9]\d)\d{4}(\d{4})/', 'replacement' => '$1****$2'],
'email' => ['pattern' => '/([^@]{0,3})[^@]*(@.+)/', 'replacement' => '$1***$2'],
'idcard' => ['pattern' => '/(\d{6})\d{8}(\d{4})/', 'replacement' => '$1********$2'],
'bankcard' => ['pattern' => '/(\d{4})\d+(\d{4})/', 'replacement' => '$1 **** **** $2'],
'name' => ['pattern' => '/(^.).(.?$)/u', 'replacement' => '$1*$2'], // 中文姓名
'default' => ['prefix' => 2, 'suffix' => 1, 'mask' => '***']
];
public function mask($value, $type = 'default') {
if (!isset($this->masks[$type])) {
$type = 'default';
}
$config = $this->masks[$type];
if (isset($config['pattern'])) {
return preg_replace($config['pattern'], $config['replacement'], $value);
}
$len = mb_strlen($value, 'utf-8');
if ($len <= $config['prefix'] + $config['suffix']) {
return str_repeat($config['mask'], $len);
}
$prefix = mb_substr($value, 0, $config['prefix'], 'utf-8');
$suffix = mb_substr($value, -$config['suffix'], null, 'utf-8');
return $prefix . $config['mask'] . $suffix;
}
}
// 使用
$masker = new DataMasker();
echo $masker->mask('13888888888', 'mobile'); // 138****8888
echo $masker->mask('test@example.com', 'email'); // tes***@example.com
echo $masker->mask('張三豐', 'name'); // 張*豐
echo $masker->mask('6225887777777777', 'bankcard'); // 6225 **** **** 7777
合規的數據脫敏不僅滿足《網絡安全法》、《個人信息保護法》要求,更能建立用戶信任,是現代 Web 應用必備的基礎能力。