Home  >  Article  >  Backend Development  >  How to hide part of a string with asterisks in PHP?

How to hide part of a string with asterisks in PHP?

coldplay.xixi
coldplay.xixiOriginal
2020-07-24 10:07:573303browse

PHP method to replace hidden characters with asterisks in a string: use the [substr_replace($AA,'*','1','2')] function to replace, where the first parameter is the required The string to be processed, the second parameter is the replacement symbol used, and the third and fourth parameters are the string positions that need to be replaced.

How to hide part of a string with asterisks in PHP?

PHP method to replace hidden characters with asterisks in a string:

Core code:

<?php 
 
/**
 +----------------------------------------------------------
 * 将一个字符串部分字符用*替代隐藏
 +----------------------------------------------------------
 * @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);
}
}
if ($type == 0) {
for ($i = $bengin; $i < ($bengin + $len); $i++) {
if (isset($array[$i]))
$array[$i] = "*";
}
$string = implode("", $array);
} else if ($type == 1) {
$array = array_reverse($array);
for ($i = $bengin; $i < ($bengin + $len); $i++) {
if (isset($array[$i]))
$array[$i] = "*";
}
$string = implode("", array_reverse($array));
} else if ($type == 2) {
$array = explode($glue, $string);
$array[0] = hideStr($array[0], $bengin, $len, 1);
$string = implode($glue, $array);
} else if ($type == 3) {
$array = explode($glue, $string);
$array[1] = hideStr($array[1], $bengin, $len, 0);
$string = implode($glue, $array);
} else if ($type == 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);
}
return $string;
}
 
 
$str = &#39;12345678901&#39;;
echo hideStr($str,2,4);

Sometimes you need to consider Chinese replacement, then you can refer to the following implementation method

For the problem of using * to replace certain parts of the string:

1. Example:

$username = "linshouyue"; 
echo substr_replace($username,&#39;****&#39;,&#39;3&#39;,&#39;4&#39;);

substr_replace()Function

  • The first parameter is the string to be processed

  • The second parameter The replacement symbol used

  • The third/fourth parameter is the string position to be replaced (replacing the last four characters starting from the third character)

However, this function has no problem with the number of English characters/digits, but once it encounters Chinese characters, it will cause very embarrassing problems, because the bytes of Chinese characters and English characters are different. You can use the following method to solve it:

/**     * *替换中文汉字
     * @author 月月
     */
    function substr_cut($user_name){
        $strlen     = mb_strlen($user_name, &#39;utf-8&#39;);
        $firstStr     = mb_substr($user_name, 0, 1, &#39;utf-8&#39;);
        $lastStr     = mb_substr($user_name, -1, 1, &#39;utf-8&#39;);
        return $strlen == 2 ? $firstStr . str_repeat(&#39;*&#39;, mb_strlen($user_name, &#39;utf-8&#39;) - 1) : $firstStr . str_repeat("*", $strlen - 2) . $lastStr;
    }

Related learning recommendations: PHP programming from entry to proficiency

The above is the detailed content of How to hide part of a string with asterisks in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn