Replace partial characters of a string with * to hide
/** * 将一个字符串部分字符用*替代隐藏 * @param string $string 待转换的字符串 * @param int $bengin 起始位置,从0开始计数,当$type=4时,表示左侧保留长度 * @param int $len 需要转换成*的字符个数,当$type=4时,表示右侧保留长度 * @param int $type 转换类型:0,从左向右隐藏;1,从右向左隐藏;2,从指定字符位置分割前由右向左隐藏;3,从指定字符位置分割后由左向右隐藏;4,保留首末指定字符串 * @param string $glue 分割符 * @return string 处理后的字符串 */ function hideStr($string, $bengin = 0, $len = 4, $type = 0, $glue = "@") { if (empty($string)) return false; $array = array(); if ($type == 0 || $type == 1 || $type == 4) { $strlen = $length = mb_strlen($string); while ($strlen) { $array[] = mb_substr($string, 0, 1, "utf8"); $string = mb_substr($string, 1, $strlen, "utf8"); $strlen = mb_strlen($string); } } switch ($type) { case 1: $array = array_reverse($array); for ($i = $bengin; $i < ($bengin + $len); $i++) { if (isset($array[$i])) $array[$i] = "*"; } $string = implode("", array_reverse($array)); break; case 2: $array = explode($glue, $string); $array[0] = hideStr($array[0], $bengin, $len, 1); $string = implode($glue, $array); break; case 3: $array = explode($glue, $string); $array[1] = hideStr($array[1], $bengin, $len, 0); $string = implode($glue, $array); break; case 4: $left = $bengin; $right = $len; $tem = array(); for ($i = 0; $i < ($length - $right); $i++) { if (isset($array[$i])) $tem[] = $i >= $left ? "*" : $array[$i]; } $array = array_chunk(array_reverse($array), $right); $array = array_reverse($array[0]); for ($i = 0; $i < $right; $i++) { $tem[] = $array[$i]; } $string = implode("", $tem); break; default: for ($i = $bengin; $i < ($bengin + $len); $i++) { if (isset($array[$i])) $array[$i] = "*"; } $string = implode("", $array); break; } return $string; }
Example 1:
$phone = '18866665555'; echo hideStr($phone,4,4);
Output
1886****888
Example 2:
$phone = '我的网站名称为PHP博客!'; $str = hideStr($phone,7,5); print_r($str);
Output:
我的网站名称为*****!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Zend Studio 13.0.1
Powerful PHP integrated development environment
