Home > Backend Development > PHP Problem > How to hide some characters in php

How to hide some characters in php

藏色散人
Release: 2023-03-04 10:48:01
Original
2513 people have browsed it

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

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!

Related labels:
php
source:php.cn
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