php中按指定标识及长度替换字符的方法代码

/**
 * 按指定标识及长度替换字符
 * @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 ''; }

使用:

默认按手机的格式:

_str_repeat('13522223333', 4, 4), 从第4位开始加*,到后面保留的位数

135****3333

_str_repeat('220181199112300078', 6, 8)220181****12300078
原文地址:https://www.cnblogs.com/sgm4231/p/9782375.html