Maison > Article > développement back-end > 怎么替换一个字符串中间的字符
怎么替换一个字符串中间的字符?
比如: fdsfdsfdsjkww
替换成:f**********w
比如: 扣分金锡民
替换成:扣***民
比如: 扣分金锡民s
替换成:扣****s
比如: tt扣分金锡民s
替换成:t*****s
<?php$str ='tt扣分金锡民s';function hide_repalce($str, $code='utf-8') { mb_internal_encoding($code); $len = mb_strlen($str); return mb_substr($str, 0, 1) . str_repeat('*', $len - 2) . mb_substr($str, $len - 1, 1);}echo hide_repalce($str);
Replace ( string1, start, string2,n,m )
<?php$str ='tt扣分金锡民s';function hide_repalce($str, $code='utf-8') { mb_internal_encoding($code); $len = mb_strlen($str); return mb_substr($str, 0, 1) . str_repeat('*', $len - 2) . mb_substr($str, $len - 1, 1);}echo hide_repalce($str);
乱码要看你的编码是什么 设置正确的编码
$string='fdsfdsfdsjkww';
echo substr($string,0,1).'*****'.substr($string,$sLeng-1,1);
<?php$str ='tt扣分金锡民s';function hide_repalce($str, $code='utf-8') { mb_internal_encoding($code); $len = mb_strlen($str); return mb_substr($str, 0, 1) . str_repeat('*', $len - 2) . mb_substr($str, $len - 1, 1);}echo hide_repalce($str);
简单点的
$string='fdsfdsfdsjkww';echo substr_replace($string, str_repeat('*',strlen($string)-2), 1, strlen($string)-2);
注意长度问题,在不同编码下,中文和英文的字节会不一样的,多以长度要计算对了,不然会出现乱码
$string='fdsfdsfdsjkww';echo substr_replace($string, str_repeat('*',strlen($string)-2), 1, strlen($string)-2);