Home >Backend Development >PHP Tutorial >How to hide some characters in a string in php

How to hide some characters in a string in php

小云云
小云云Original
2018-03-03 15:06:224668browse

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, hoping to help Everyone.

// 隐藏部分字符串
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;
}

Related recommendations:

Use PHP to hide strings and specify position strings

Convert a character Some characters in the string are hidden with * instead of *

php Some characters are hidden with * and some problems are related with string replacement

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