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

小云云
Release: 2023-03-20 18:00:02
Original
4842 people have browsed it

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;
}
Copy after login

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!

Related labels:
Statement of this Website
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template