车牌、手机、身份证、等敏感信息 屏蔽 替换 、中文转unicode编码 函数
应工作要求,需要对展示的内容进行敏感信息替换。
琢磨的一些时间,编写的函数匹配率还是比较高的。
顺便说下思路,使用的是正则匹配替换和字符串替换。函数可以再改进.
先把需要匹配的内容写好相应的正则,然后进行全部匹配遍历。
获取匹配结果数组A,同时获取匹配规则B,(解决内容有多次符合匹配规则的情况,导致最后替换出现结果一样的问题)
再根据结果A,和结果B,进行内容的真正匹配处理。
代码:
<?php /** * 截取内容中的数字,替换为其他 * @param type $string * @param type $lengths * @param type $padString * @return boolean */ function replace_numeral($string, $lengths = array(), $padString = '*') { if (!$string || !$lengths) { return false; } $lengths = !is_array($lengths) ? array($lengths) : $lengths; $pattern = $strings = array(); rsort(&$lengths, SORT_NUMERIC); foreach ($lengths as $length) { preg_match_all('/\d{' . ((int) $length - 1) . '}[\d|x|X]/', $string, $matches); if (isset($matches[0]) && $matches[0]) { foreach ($matches[0] as $k => $v) { if ($v) { $pattern[$k] = '/' . $v . '/'; $strings[$k] = $v ? str_pad($padString, $length - 4, $padString) . substr($v, -4, 4) : ''; } } } $string = preg_replace($pattern, $strings, $string); } return $string; } /** * 替换邮箱 * @param type $string * @param type $length * @return boolean */ function replace_mail($string, $length = 3) { if (!$string) { return false; } preg_match_all('/([a-z0-9]*[-_\.]?[a-z0-9]+)*@([a-z0-9]*[-_]?[a-z0-9]+)+[\.][a-z]{2,3}([\.][a-z]{2})?/', $string, $matches); foreach ($matches[0] as $k => $v) { if (isset($matches[0]) && $matches[0]) { foreach ($matches[0] as $k => $v) { if ($v) { $pattern[$k] = $v; $strings[$k] = substr_replace($v, str_pad('*', strpos($v, '@') - $length * 2, '*'), $length, strpos($v, '@') - $length * 2); } } } } $string = str_replace($pattern, $strings, $string); return ($string); } /** * 替换车牌 * @param type $string * @return type */ function replace_vehicle_reg_no($string) { $arr_area = array('赣', '桂', '京', '津', '冀', '晋', '蒙', '辽', '吉', '黑', '沪', '苏', '浙', '皖', '闽', '鲁', '豫', '鄂', '湘', '粤', '琼', '渝', '川', '贵', '云', '藏', '陕', '甘', '青', '宁', '新'); foreach ($arr_area AS $k => $v) { preg_match_all('/' . $v . '[a-zA-Z]{1}[a-zA-Z0-9]{5}/', $string, $matches); if (isset($matches[0]) && $matches[0]) { foreach ($matches[0] as $va) { if ($va) { $pattern[$k] = '/' . $va . '/'; $strings[$k] = substr_replace($va, '**', -4, 2); } } $string = preg_replace($pattern, $strings, $string); } } return $string; } /** * 替换车架号 * @param type $string * @param type $lengths * @param type $padString * @return boolean */ function replace_vin($string) { if (!$string) { return false; } $pattern = $strings = array(); preg_match_all('/[a-zA-Z0-9]{1,11}[0-9]{5}[\d|x|X]/', $string, $matches); if (isset($matches[0]) && $matches[0]) { foreach ($matches[0] as $k => $v) { if ($v) { $pattern[$k] = '/' . $v . '/'; $strings[$k] = substr_replace($v, '******', 6, 5); } } } $string = preg_replace($pattern, $strings, $string); return $string; } ?>
再弄一个转换汉字为unicode编码(十六进制)的函数
<?php function unicode_encode($name) { $name = iconv('UTF-8', 'UCS-2', $name); $len = strlen($name); $str = ''; for ($i = 0; $i < $len - 1; $i = $i + 2) { $c = $name[$i]; $c2 = $name[$i + 1]; if (ord($c) > 0) { // 两个字节的文字 $str .= '\u' . base_convert(ord($c), 10, 16) . base_convert(ord($c2), 10, 16); } else { $str .= $c2; } } return $str; } function unicode_decode($name) { // 转换编码,将Unicode编码转换成可以浏览的utf-8编码 $pattern = '/([\w]+)|(\\\u([\w]{4}))/i'; preg_match_all($pattern, $name, $matches); if (!empty($matches)) { $name = ''; for ($j = 0; $j < count($matches[0]); $j++) { $str = $matches[0][$j]; if (strpos($str, '\\u') === 0) { $code = base_convert(substr($str, 2, 2), 16, 10); $code2 = base_convert(substr($str, 4), 16, 10); $c = chr($code) . chr($code2); $c = iconv('UCS-2', 'UTF-8', $c); $name .= $c; } else { $name .= $str; } } } return $name; } ?>