Home  >  Article  >  Backend Development  >  How to hide some characters in php

How to hide some characters in php

藏色散人
藏色散人Original
2020-08-12 10:35:112389browse

How to implement partial character hiding in php: First create a PHP sample file; then define a "func_substr_replace" method; then use "mb_strlen" and other functions to hide some characters in the middle of the string.

How to hide some characters in php

Recommended: "PHP Video Tutorial"

php hides the middle characters of the string

Often when the winning list is announced, in order to avoid exposing the winner’s mobile phone number, the middle part of the number is hidden, such as: 139****2972. This article simply shares a piece of PHP implementation code. Great God It can be ignored and the rookie continues.

// 隐藏部分字符串
function func_substr_replace($str, $replacement = '*', $start = 1, $length = 3)
{
    $len = mb_strlen($str,'utf-8');
    if ($len > intval($start+$length)) {
        $str1 = mb_substr($str,0,$start,'utf-8');
        $str2 = mb_substr($str,intval($start+$length),NULL,'utf-8');
    } else {
        $str1 = mb_substr($str,0,1,'utf-8');
        $str2 = mb_substr($str,$len-1,1,'utf-8');    
        $length = $len - 2;        
    }
    $new_str = $str1;
    for ($i = 0; $i < $length; $i++) { 
        $new_str .= $replacement;
    }
    $new_str .= $str2;
    return $new_str;
}

The above is the detailed content of How to hide some characters 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