Home  >  Article  >  Backend Development  >  Method code for replacing characters according to specified identifier and length in php

Method code for replacing characters according to specified identifier and length in php

不言
不言Original
2018-08-22 15:50:151461browse

The content of this article is about the method code of replacing characters according to the specified logo and length in PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

/**
 * 按指定标识及长度替换字符
 * @param $str
 * @param int $start   开始的位数
 * @param int $end     后面保留的位数
 * @param string $mode
 * @return mixed
 */function _str_repeat($str=null, $start = 4, $end = 4, $mode = '*'){
    if(!empty($str)){        
    $length = mb_strlen($str,'utf8')-$start-$end;        
    $repeat = str_repeat($mode, $length);    
    // 按个数输出标识

        return substr_replace($str, $repeat, $start, $length);
    }    
    return '';
}

Use

默认按手机的格式:
_str_repeat('13522223333', 4, 4), 从第4位开始加*,到后面保留的位数
135****3333
_str_repeat('220181199112300078', 6, 8)220181****12300078

Related recommendations:

php code to intercept a string according to the specified length

php function to convert numbers to strings of specified length

The above is the detailed content of Method code for replacing characters according to specified identifier and length 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